ExcelBase.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. 
  2. using System;
  3. using System.IO;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using NPOI.SS.UserModel;
  7. using NPOI.HSSF.UserModel;
  8. using NPOI.XSSF.UserModel;
  9. using Utilty.Unit.File;
  10. using Utilty.Unit.Enumerable;
  11. namespace Utilty.Module.File.Source.Base
  12. {
  13. /// <summary>
  14. /// Excel模块基类
  15. /// </summary>
  16. internal class ExcelBase
  17. {
  18. private readonly string Format1 = ".xlsx";
  19. private readonly string Format2 = ".xls";
  20. /// <summary>
  21. /// 自定义需求
  22. /// </summary>
  23. protected Action<ISheet> CustomDemandAction { get; set; }
  24. /// <summary>
  25. /// 将数据写入到Excel并保存到指定目录
  26. /// </summary>
  27. /// <param name="excelPath">excel路劲</param>
  28. /// <param name="excelData">写入EXCEL的数据字典,key为sheet名称,value为sheet数据</param>
  29. public void CreateExcel(string excelPath, Dictionary<string, List<List<string>>> excelData)
  30. {
  31. IWorkbook workbook = CreateWorkbook();
  32. foreach (var sheet in excelData)
  33. {
  34. WriteWorkSheet(workbook.CreateSheet(sheet.Key), sheet.Value);
  35. }
  36. SaveExcel();
  37. IWorkbook CreateWorkbook()
  38. {
  39. string format = Path.GetExtension(excelPath);
  40. if (format == Format1)
  41. return new XSSFWorkbook();
  42. else if (format == Format2)
  43. return new HSSFWorkbook();
  44. throw new Exception($"不支持{format}格式");
  45. }
  46. void SaveExcel()
  47. {
  48. using FileStream fs = FileUnit.CheckCreate(excelPath);
  49. workbook.Write(fs);
  50. workbook.Close();
  51. }
  52. }
  53. /// <summary>
  54. /// 将数据写入到Sheet
  55. /// </summary>
  56. /// <param name="worksheet">表</param>
  57. /// <param name="sheetData">表数据</param>
  58. private void WriteWorkSheet(ISheet worksheet, List<List<string>> sheetData)
  59. {
  60. sheetData.ForEach((rowData, rowIndex) =>
  61. {
  62. IRow row = worksheet.CreateRow(rowIndex);
  63. rowData.ForEach((cellData, cellIndex) =>
  64. {
  65. ICell cell = row.CreateCell(cellIndex);
  66. cell.SetCellValue(cellData);
  67. });
  68. });
  69. worksheet.DefaultColumnWidth = 20;
  70. CustomDemandAction?.Invoke(worksheet);
  71. }
  72. /// <summary>
  73. /// 读取指定excel文件流的数据
  74. /// </summary>
  75. /// <param name="file">excel文件流</param>
  76. /// <param name="fileExtension">excel文件格式(.xls、.xlsx)</param>
  77. /// <param name="excelCover">excel转换数据的映射信息</param>
  78. /// <returns></returns>
  79. public Dictionary<string, List<List<string>>> ReadExcel(Stream file,string fileExtension, Dictionary<string,(List<string>,int)> excelCover)
  80. {
  81. Dictionary<string, List<List<string>>> excelData = new Dictionary<string, List<List<string>>>();
  82. IWorkbook workbook = ReadWorkbook();
  83. foreach (var sheetCover in excelCover)
  84. {
  85. var (header, headerIndex) = sheetCover.Value;
  86. ISheet worksheet = workbook.GetSheet(sheetCover.Key);
  87. excelData.Add(sheetCover.Key,ReadWorkSheet(worksheet, header, headerIndex));
  88. }
  89. return excelData;
  90. IWorkbook ReadWorkbook()
  91. {
  92. if (fileExtension == Format1)
  93. return new XSSFWorkbook(file);
  94. else if(fileExtension == Format2)
  95. return new HSSFWorkbook(file);
  96. throw new Exception($"不支持{fileExtension}格式");
  97. }
  98. }
  99. /// <summary>
  100. /// 读取sheet里的数据
  101. /// </summary>
  102. /// <param name="worksheet">表</param>
  103. /// <param name="header">数据头部</param>
  104. /// <param name="headerIndex">头部所在索引</param>
  105. /// <returns></returns>
  106. private List<List<string>> ReadWorkSheet(ISheet worksheet, List<string> header, int headerIndex)
  107. {
  108. var sheetData = new List<List<string>>();
  109. var (keys, indexs) = GetHeaderMap();
  110. sheetData.Add(keys);
  111. IEnumerator rows = worksheet.GetRowEnumerator();
  112. while (rows.MoveNext())
  113. {
  114. var rowData = new List<string>();
  115. IRow row = rows.Current as IRow;
  116. foreach (var index in indexs)
  117. {
  118. rowData.Add(row.GetCell(index).StringCellValue);
  119. }
  120. sheetData.Add(rowData);
  121. }
  122. return sheetData;
  123. (List<string>, List<int>) GetHeaderMap()
  124. {
  125. var keys = new List<string>();
  126. var indexs = new List<int>();
  127. IRow headerRow = worksheet.GetRow(headerIndex);
  128. headerRow.Cells.ForEach(cell =>
  129. {
  130. if (header.Contains(cell.StringCellValue))
  131. {
  132. keys.Add(cell.StringCellValue);
  133. indexs.Add(cell.ColumnIndex);
  134. }
  135. });
  136. worksheet.RemoveRow(headerRow);
  137. return (keys,indexs);
  138. }
  139. }
  140. }
  141. }