ExcelUtils.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. using Senparc.Weixin.MP.AdvancedAPIs.Card;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System;
  5. using NPOI.SS.UserModel;
  6. using NPOI.XSSF.UserModel;
  7. using NPOI.HSSF.UserModel;
  8. using System.Reflection;
  9. using System.Data;
  10. namespace JiaZhiQuan.Common.Utils
  11. {
  12. public class ExcelUtils
  13. {
  14. public static byte[] ExportToExcel(List<Dictionary<string, object>> data, List<string> columnNames)
  15. {
  16. IWorkbook workbook = new XSSFWorkbook();
  17. ISheet sheet = workbook.CreateSheet("Sheet1");
  18. ICreationHelper creationHelper = workbook.GetCreationHelper();
  19. ICellStyle dateCellStyle = CreateDateCellStyle(workbook);
  20. ICellStyle numberCellStyle = CreateNumberCellStyle(workbook);
  21. ICellStyle doubleCellStyle = CreateDoubleCellStyle(workbook);
  22. // Create header row
  23. IRow headerRow = sheet.CreateRow(0);
  24. if (columnNames != null && columnNames.Count > 0)
  25. {
  26. for (int colIndex = 0; colIndex < columnNames.Count; colIndex++)
  27. {
  28. headerRow.CreateCell(colIndex).SetCellValue(columnNames[colIndex]);
  29. }
  30. }
  31. else
  32. {
  33. // If columnNames is not provided, use keys from the first dictionary as column names
  34. if (data.Count > 0)
  35. {
  36. var firstRow = data[0];
  37. int colIndex = 0;
  38. foreach (var columnName in firstRow.Keys)
  39. {
  40. headerRow.CreateCell(colIndex).SetCellValue(columnName);
  41. colIndex++;
  42. }
  43. }
  44. }
  45. // Create data rows
  46. int rowIndex = 1;
  47. foreach (var rowData in data)
  48. {
  49. IRow dataRow = sheet.CreateRow(rowIndex);
  50. int colIndex = 0;
  51. foreach (var value in rowData.Values)
  52. {
  53. ICell cell = dataRow.CreateCell(colIndex);
  54. if (value != null)
  55. {
  56. Type valueType = value.GetType();
  57. if (valueType == typeof(DateTime))
  58. {
  59. cell.SetCellValue((DateTime)value);
  60. cell.CellStyle = dateCellStyle;
  61. }
  62. else if (valueType.IsEnum)
  63. {
  64. cell.SetCellValue(Enum.GetName(valueType, value));
  65. }
  66. else if (valueType == typeof(bool))
  67. {
  68. cell.SetCellValue((bool)value);
  69. }
  70. else if (IsNumericType(valueType))
  71. {
  72. cell.SetCellValue(Convert.ToInt64(value));
  73. cell.CellStyle = numberCellStyle;
  74. }
  75. else if (IsDoubleType(valueType))
  76. {
  77. cell.SetCellValue(Convert.ToDouble(value));
  78. cell.CellStyle = doubleCellStyle;
  79. }
  80. else
  81. {
  82. cell.SetCellValue(creationHelper.CreateRichTextString(value.ToString()));
  83. }
  84. }
  85. colIndex++;
  86. }
  87. rowIndex++;
  88. }
  89. for (int i = 0; i < headerRow.Cells.Count; i++)
  90. {
  91. sheet.AutoSizeColumn(i);
  92. }
  93. using MemoryStream stream = new MemoryStream();
  94. workbook.Write(stream);
  95. return stream.ToArray();
  96. }
  97. public static byte[] ExportToExcel<T>(List<T> data)
  98. {
  99. IWorkbook workbook = new XSSFWorkbook();
  100. ISheet sheet = workbook.CreateSheet("Sheet1");
  101. ICreationHelper creationHelper = workbook.GetCreationHelper();
  102. ICellStyle dateCellStyle = CreateDateCellStyle(workbook);
  103. ICellStyle numberCellStyle = CreateNumberCellStyle(workbook);
  104. ICellStyle doubleCellStyle = CreateDoubleCellStyle(workbook);
  105. // Create header row
  106. IRow headerRow = sheet.CreateRow(0);
  107. var properties = typeof(T).GetProperties();
  108. for (int i = 0; i < properties.Length; i++)
  109. {
  110. var property = properties[i];
  111. var attr = GetAttr(property);
  112. string columnName = attr?.ColumnName ?? property.Name;
  113. headerRow.CreateCell(i).SetCellValue(columnName);
  114. }
  115. // Create data rows
  116. for (int rowIndex = 0; rowIndex < data.Count; rowIndex++)
  117. {
  118. IRow dataRow = sheet.CreateRow(rowIndex + 1);
  119. for (int colIndex = 0; colIndex < properties.Length; colIndex++)
  120. {
  121. var value = properties[colIndex].GetValue(data[rowIndex]);
  122. ICell cell = dataRow.CreateCell(colIndex);
  123. if (value != null)
  124. {
  125. Type valueType = value.GetType();
  126. if (valueType == typeof(DateTime))
  127. {
  128. cell.SetCellValue((DateTime)value);
  129. cell.CellStyle = dateCellStyle;
  130. }
  131. else if (valueType.IsEnum)
  132. {
  133. cell.SetCellValue(Enum.GetName(valueType, value));
  134. }
  135. else if (valueType == typeof(bool))
  136. {
  137. cell.SetCellValue((bool)value);
  138. }
  139. else if (IsNumericType(valueType))
  140. {
  141. cell.SetCellValue(Convert.ToInt64(value));
  142. cell.CellStyle = numberCellStyle;
  143. }
  144. else if (IsDoubleType(valueType))
  145. {
  146. cell.SetCellValue(Convert.ToDouble(value));
  147. cell.CellStyle = doubleCellStyle;
  148. }
  149. else
  150. {
  151. cell.SetCellValue(creationHelper.CreateRichTextString(value.ToString()));
  152. }
  153. }
  154. }
  155. }
  156. for (int i = 0; i < properties.Length; i++)
  157. {
  158. sheet.AutoSizeColumn(i);
  159. }
  160. using MemoryStream stream = new MemoryStream();
  161. workbook.Write(stream);
  162. return stream.ToArray();
  163. }
  164. public static ExcelConfigAttribute GetAttr(PropertyInfo property)
  165. {
  166. return (ExcelConfigAttribute)Attribute.GetCustomAttribute(property, typeof(ExcelConfigAttribute));
  167. }
  168. public static ICellStyle CreateDateCellStyle(IWorkbook workbook)
  169. {
  170. ICellStyle cellStyle = workbook.CreateCellStyle();
  171. IDataFormat dateFormat = workbook.CreateDataFormat();
  172. cellStyle.DataFormat = dateFormat.GetFormat("yyyy-mm-dd hh:mm:ss");
  173. return cellStyle;
  174. }
  175. public static ICellStyle CreateNumberCellStyle(IWorkbook workbook)
  176. {
  177. ICellStyle cellStyle = workbook.CreateCellStyle();
  178. cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0");
  179. return cellStyle;
  180. }
  181. public static ICellStyle CreateDoubleCellStyle(IWorkbook workbook)
  182. {
  183. ICellStyle cellStyle = workbook.CreateCellStyle();
  184. cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00");
  185. return cellStyle;
  186. }
  187. public static bool IsNumericType(Type valueType)
  188. {
  189. return valueType.IsPrimitive && valueType != typeof(bool) ||
  190. valueType == typeof(int) ||
  191. valueType == typeof(long) ||
  192. valueType == typeof(short) ||
  193. valueType == typeof(byte);
  194. }
  195. public static bool IsDoubleType(Type valueType)
  196. {
  197. return valueType.IsPrimitive && valueType != typeof(bool) ||
  198. valueType == typeof(decimal) ||
  199. valueType == typeof(double) ||
  200. valueType == typeof(float);
  201. }
  202. }
  203. public class ExcelColumnDTO
  204. {
  205. /// <summary>
  206. /// 数据的字段名称
  207. /// </summary>
  208. public string Prop { get; set; }
  209. /// <summary>
  210. /// Excel的列名
  211. /// </summary>
  212. public string Label { get; set; }
  213. }
  214. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Property, AllowMultiple = false)]
  215. public class ExcelConfigAttribute : Attribute
  216. {
  217. public string ColumnName { get; }
  218. public ExcelConfigAttribute(string columnName)
  219. {
  220. ColumnName = columnName;
  221. }
  222. }
  223. }