ExcelHelper.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using UnityEngine;
  5. using System.IO;
  6. using ExcelDataReader;
  7. using System.Reflection;
  8. using OfficeOpenXml;
  9. /// <summary>
  10. /// Excel帮助类
  11. /// </summary>
  12. public class ExcelHelper
  13. {
  14. /// <summary>
  15. /// 从Excel文件读取信息
  16. /// </summary>
  17. /// <param name="tableName">文件路径名(需要有文件后缀)</param>
  18. /// <param name="headCount">表头</param>
  19. /// <returns></returns>
  20. public static List<T> ReadInfoFromExcel<T>(string filedPath, int headCount)
  21. {
  22. List<T> tmpTs = new List<T>();
  23. using (FileStream fileStream = File.Open(filedPath, FileMode.Open, FileAccess.Read))
  24. {
  25. using (IExcelDataReader dataReader = ExcelReaderFactory.CreateReader(fileStream))
  26. {
  27. //拿Excel第一张表
  28. DataTable tmpDataTable = dataReader.AsDataSet().Tables[0];
  29. //类中变量
  30. FieldInfo[] tmpFileInfos = typeof(T).GetFields();
  31. //匹配数据
  32. if (tmpFileInfos.Length != tmpDataTable.Columns.Count)
  33. {
  34. Debug.LogError("tmpFileInfos.Length:"+ tmpFileInfos.Length);
  35. Debug.LogError("tmpDataTable.Columns.Count:"+ tmpDataTable.Columns.Count);
  36. Debug.LogError($"转换数据与{filedPath}的元素数量不相同,请检查!!!");
  37. return tmpTs;
  38. }
  39. for (int i = headCount; i < tmpDataTable.Rows.Count; i++)
  40. {
  41. T tmpT = Activator.CreateInstance<T>();
  42. for (int j = 0; j < tmpFileInfos.Length; j++)
  43. {
  44. FieldInfo tmpFieldInfo = tmpFileInfos[j];
  45. tmpFieldInfo.SetValue(tmpT, tmpDataTable.Rows[i][j].ToString());
  46. }
  47. tmpTs.Add(tmpT);
  48. }
  49. }
  50. }
  51. return tmpTs;
  52. }
  53. /// <summary>
  54. /// 将数据信息写入到Excel文件中
  55. /// </summary>
  56. /// <typeparam name="T">类型</typeparam>
  57. /// <param name="ts">信息</param>
  58. /// <param name="filePath">全路径+后缀</param>
  59. public static bool WriteInfoToExcel<T>(string filePath, List<T> ts)
  60. {
  61. using (ExcelPackage package = new ExcelPackage())
  62. {
  63. if (package.Workbook.Worksheets.Count == 0) package.Workbook.Worksheets.Add("Sheet1");
  64. ExcelWorksheet sheet = package.Workbook.Worksheets["Sheet1"];
  65. FieldInfo[] tmpFieldInfos = typeof(T).GetFields();
  66. //写入标题
  67. for (int i = 1; i < tmpFieldInfos.Length + 1; i++) sheet.Cells[1, i].Value = tmpFieldInfos[i - 1].Name;
  68. //写入数据
  69. for (int i = 2; i < ts.Count + 2; i++)
  70. {
  71. for (int j = 1; j < tmpFieldInfos.Length + 1; j++)
  72. {
  73. sheet.Cells[i, j].Value = tmpFieldInfos[j - 1].GetValue(ts[i - 2]);
  74. }
  75. }
  76. FileInfo tmpFileInfo = new FileInfo(filePath);
  77. //如果不存在表格就创建表格
  78. if (!tmpFileInfo.Exists) tmpFileInfo.Create().Dispose();
  79. package.SaveAs(tmpFileInfo);
  80. }
  81. return false;
  82. }
  83. public static bool WriteInfoToExcel<T>(string filePath, T ts)
  84. {
  85. FileInfo tmpFileInfo = new FileInfo(filePath);
  86. ExcelPackage package;
  87. if (!tmpFileInfo.Exists || tmpFileInfo.Length == 0)
  88. package = new ExcelPackage();
  89. else
  90. package = new ExcelPackage(tmpFileInfo);
  91. using (package)
  92. {
  93. ExcelWorksheet sheet;
  94. if (package.Workbook.Worksheets.Count == 0) sheet = package.Workbook.Worksheets.Add("Sheet1");
  95. else sheet = package.Workbook.Worksheets["Sheet1"];
  96. FieldInfo[] tmpFieldInfos = typeof(T).GetFields();
  97. // 确定起始行(如果是新文件从第1行开始,追加模式从最后一行+1开始)
  98. int startRow = sheet.Dimension?.End.Row + 1 ?? 1;
  99. // 写入数据
  100. for (int i = 1; i < tmpFieldInfos.Length + 1; i++)
  101. {
  102. sheet.Cells[startRow, i].Value = tmpFieldInfos[i - 1].GetValue(ts);
  103. }
  104. package.SaveAs(tmpFileInfo);
  105. }
  106. return false;
  107. }
  108. }