基础服务api
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

109 wiersze
3.8 KiB

  1. using Microsoft.VisualBasic;
  2. using Npoi.Mapper;
  3. using NPOI.SS.UserModel;
  4. using NPOI.XSSF.UserModel;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Data;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace BPA.SAAS.Manage.Comm.Util
  12. {
  13. public class ReadExcel<T> where T : class, new()
  14. {
  15. /// <summary>
  16. /// 导入xlsx
  17. /// </summary>
  18. /// <param name="stream"></param>
  19. /// <returns></returns>
  20. public List<T> RedXml(Stream stream)
  21. {
  22. DataTable dtTable = new DataTable();
  23. List<string> rowList = new List<string>();
  24. ISheet sheet;
  25. stream.Position = 0;
  26. XSSFWorkbook xssWorkbook = new XSSFWorkbook(stream);
  27. sheet = xssWorkbook.GetSheetAt(0);
  28. IRow headerRow = sheet.GetRow(0);
  29. int cellCount = headerRow.LastCellNum;
  30. for (int j = 0; j < cellCount; j++)
  31. {
  32. ICell cell = headerRow.GetCell(j);
  33. if (cell == null || string.IsNullOrWhiteSpace(cell.ToString())) continue;
  34. {
  35. dtTable.Columns.Add(cell.ToString());
  36. }
  37. }
  38. for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)
  39. {
  40. IRow row = sheet.GetRow(i);
  41. if (row == null) continue;
  42. if (row.Cells.All(d => d.CellType == CellType.Blank)) continue;
  43. for (int j = row.FirstCellNum; j < cellCount; j++)
  44. {
  45. if (row.GetCell(j) != null)
  46. {
  47. if (!string.IsNullOrEmpty(row.GetCell(j).ToString()) && !string.IsNullOrWhiteSpace(row.GetCell(j).ToString()))
  48. {
  49. rowList.Add(row.GetCell(j).ToString());
  50. }
  51. }
  52. }
  53. if (rowList.Count > 0)
  54. dtTable.Rows.Add(rowList.ToArray());
  55. rowList.Clear();
  56. }
  57. var data = DataTableUitity.DatatTableToList<T>(dtTable);
  58. return data;
  59. }
  60. /// <summary>
  61. /// Excel转为List
  62. /// </summary>
  63. /// <typeparam name="T"></typeparam>
  64. /// <param name="fileStream"></param>
  65. /// <param name="sheetname"></param>
  66. /// <returns></returns>
  67. public List<T> ExcelToList(Stream fileStream, Mapper mapper, string sheetname = "")
  68. {
  69. List<T> ModelList = new List<T>();
  70. // var mapper = new Mapper(fileStream);
  71. List<RowInfo<T>> DataList = new List<RowInfo<T>>();
  72. if (!string.IsNullOrEmpty(sheetname))
  73. {
  74. DataList = mapper.Take<T>(sheetname).ToList();
  75. }
  76. else
  77. {
  78. DataList = mapper.Take<T>().ToList();
  79. }
  80. if (DataList != null && DataList.Count > 0)
  81. {
  82. foreach (var item in DataList)
  83. {
  84. ModelList.Add(item.Value);
  85. }
  86. }
  87. return ModelList;
  88. }
  89. /// <summary>
  90. /// List转Excel
  91. /// </summary>
  92. /// <typeparam name="T"></typeparam>
  93. /// <param name="list">数据</param>
  94. /// <param name="sheetName">表名</param>
  95. /// <param name="overwrite">true,覆盖单元格,false追加内容(list和创建的excel或excel模板)</param>
  96. /// <param name="xlsx">true-xlsx,false-xls</param>
  97. /// <returns>返回文件</returns>
  98. public MemoryStream ListToExcel(List<T> list, string sheetName = "sheet1", bool overwrite = true, bool xlsx = true)
  99. {
  100. var mapper = new Mapper();
  101. MemoryStream ms = new MemoryStream();
  102. mapper.Save<T>(ms, list, sheetName, overwrite, xlsx);
  103. return ms;
  104. }
  105. }
  106. }