ExcelHelper.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using XGame.Framework.Asset;
  5. using XGame.Framework.Asset.Addressable;
  6. using XGame.Framework.Asset.Addressable.Data;
  7. using NPOI.HSSF.UserModel;
  8. using NPOI.HSSF.Util;
  9. using NPOI.SS.UserModel;
  10. using NPOI.XSSF.UserModel;
  11. using UnityEditor;
  12. using UnityEngine;
  13. namespace XGame.Editor.Asset
  14. {
  15. public class ExcelHelper
  16. {
  17. public static void AddressableAssetsToExcel(AddressableClassify classify)
  18. {
  19. AddressableAssetsToExcel(AddressableHelper.LoadAssetManifest(classify));
  20. }
  21. public static void AddressableAssetsToExcel(AddressableInfosSo manifest)
  22. {
  23. if (manifest == null)
  24. {
  25. Debug.LogError("AddressableAssetInfoManifest is null.");
  26. return;
  27. }
  28. var filePath = EditorUtility.SaveFilePanel("Save Addressable Asset Manifest", Application.dataPath, "AddressableAsset", "xlsx");
  29. if (string.IsNullOrEmpty(filePath))
  30. {
  31. return;
  32. }
  33. var assetInfos = manifest.GetAssetInfos();
  34. assetInfos.Sort(((info0, info1) => string.Compare(info0.GetAssetPath(), info1.GetAssetPath(), StringComparison.CurrentCultureIgnoreCase)));
  35. IWorkbook workbook = null;
  36. //若文件已存在
  37. //if (File.Exists(filePath))
  38. //根据指定的文件格式创建对应的类
  39. if (Path.GetExtension(filePath).Equals(".xls"))
  40. {
  41. workbook = new HSSFWorkbook();
  42. }
  43. else
  44. {
  45. workbook = new XSSFWorkbook();
  46. }
  47. //创建表单
  48. //所有资源表单
  49. CreateSheet(workbook, "所有资源", assetInfos);
  50. //名字不匹配的资源表单
  51. var count = 0;
  52. var mismatchAssetInfos = assetInfos.FindAll((info => {
  53. EditorUtility.DisplayProgressBar("收集资源中...", info.GetAssetPath(), (++count) / (float)assetInfos.Count);
  54. return AddressableHelper.IsValidPath(info.GetAssetPath()) && !AddressableHelper.IsDefaultAddressableName(info.addressableName, info.assetGUID);
  55. }));
  56. EditorUtility.ClearProgressBar();
  57. CreateSheet(workbook, "名字不匹配的资源", mismatchAssetInfos);
  58. try
  59. {
  60. FileStream fs = File.OpenWrite(filePath);
  61. workbook.Write(fs);//向打开的这个Excel文件中写入表单并保存。
  62. fs.Close();
  63. }
  64. catch (Exception e)
  65. {
  66. Debug.LogException(e);
  67. }
  68. Debug.Log("AddressableAssetsToExcel finish.");
  69. }
  70. private static void CreateSheet(IWorkbook workbook, string sheetName, List<AssetInfo> assetInfos)
  71. {
  72. //创建一个表单
  73. ISheet sheet = workbook.CreateSheet(sheetName);
  74. //标题格式
  75. var stytleTitle = CreateTitleStytle(workbook);
  76. IRow row = sheet.CreateRow(0);
  77. ICell cell;
  78. //设置列宽
  79. int[] columnWidth = { 128, 32 };
  80. string[] titles = { "Asset Path", "Addressable Name" };
  81. int colCount = columnWidth.Length;
  82. for (int i = 0; i < colCount; i++)
  83. {
  84. //设置列宽度,256*字符数,因为单位是1/256个字符
  85. sheet.SetColumnWidth(i, 256 * columnWidth[i]);
  86. cell = row.CreateCell(i);
  87. cell.CellStyle = stytleTitle;
  88. SetCellValue(cell, titles[i]);
  89. }
  90. var stringStytle = CreateStringStytle(workbook);
  91. var rowCount = assetInfos.Count;
  92. for (int i = 0; i < rowCount; i++)
  93. {
  94. var assetInfo = assetInfos[i];
  95. row = sheet.CreateRow(i + 1);
  96. //path
  97. cell = row.CreateCell(0);
  98. cell.CellStyle = stringStytle;
  99. SetCellValue(cell, assetInfo.GetAssetPath());
  100. //名字
  101. cell = row.CreateCell(1);
  102. cell.CellStyle = stringStytle;
  103. SetCellValue(cell, assetInfo.addressableName);
  104. }
  105. }
  106. private static ICellStyle CreateTitleStytle(IWorkbook wb)
  107. {
  108. //标题的样式
  109. var style = wb.CreateCellStyle();
  110. style.Alignment = HorizontalAlignment.Center;//文字水平对齐方式
  111. style.VerticalAlignment = VerticalAlignment.Center;//文字垂直对齐方式
  112. //设置边框
  113. style.BorderBottom = BorderStyle.Thin;
  114. style.BorderLeft = BorderStyle.Thin;
  115. style.BorderRight = BorderStyle.Thin;
  116. style.BorderTop = BorderStyle.Thin;
  117. style.WrapText = true;//自动换行
  118. //style.IsLocked = false;//设置该单元格为非锁定
  119. IFont font = wb.CreateFont();//字体
  120. font.FontName = "楷体";
  121. font.Color = HSSFColor.Black.Index;//字体颜色
  122. font.Boldweight = (short)FontBoldWeight.Bold;//字体加粗样式
  123. style.SetFont(font);//样式里的字体设置具体的字体样式
  124. return style;
  125. }
  126. private static ICellStyle CreateStringStytle(IWorkbook wb)
  127. {
  128. ICellStyle style = wb.CreateCellStyle();//样式
  129. style.Alignment = HorizontalAlignment.Left;//文字水平对齐方式
  130. style.VerticalAlignment = VerticalAlignment.Center;//文字垂直对齐方式
  131. IFont font = wb.CreateFont();//字体
  132. font.FontName = "楷体";
  133. font.Color = HSSFColor.Black.Index;//字体颜色
  134. font.Boldweight = (short)FontBoldWeight.Normal;//字体加粗样式
  135. style.SetFont(font);//样式里的字体设置具体的字体样式
  136. //设置背景色
  137. //style.FillForegroundColor = HSSFColor.Yellow.Index;
  138. //style.FillPattern = FillPattern.SolidForeground;
  139. //style.FillBackgroundColor = HSSFColor.Yellow.Index;
  140. return style;
  141. }
  142. /// <summary>
  143. /// 根据数据类型设置不同类型的cell
  144. /// </summary>
  145. /// <param name="cell"></param>
  146. /// <param name="obj"></param>
  147. private static void SetCellValue(ICell cell, object obj)
  148. {
  149. var objType = obj.GetType();
  150. if (objType == typeof(int))
  151. {
  152. cell.SetCellValue((int)obj);
  153. }
  154. else if (objType == typeof(double))
  155. {
  156. cell.SetCellValue((double)obj);
  157. }
  158. else if (objType == typeof(IRichTextString))
  159. {
  160. cell.SetCellValue((IRichTextString)obj);
  161. }
  162. else if (objType == typeof(string))
  163. {
  164. cell.SetCellValue(obj.ToString());
  165. }
  166. else if (objType == typeof(DateTime))
  167. {
  168. cell.SetCellValue((DateTime)obj);
  169. }
  170. else if (objType == typeof(bool))
  171. {
  172. cell.SetCellValue((bool)obj);
  173. }
  174. else
  175. {
  176. cell.SetCellValue(obj.ToString());
  177. }
  178. }
  179. }
  180. }