技术共享

.net开发:NPOI生成excel文件到磁盘

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

源码实测可用

使用.net工具包NPOI,生成excel文件到本地磁盘。

实际项目中可以指定路径到服务器,把生成的文件存放到服务器指定目录。

controller层

  1. [HttpPost("ExportExcel")]
  2. public void ExportExcel()
  3. {
  4. _TestService.ExportToExcel();
  5. }

service层

  1. public void ExportToExcel() {
  2. String FileName = "D:\learning\yxl\chinese\2024年7月10日星期三.xlsx";
  3. var chineseWordList = this.WrapChineseWord();
  4. ExportExcelToDiskUtils<ChineseWord>.ExportToExcel(chineseWordList, FileName);
  5. }

NPOI工具类

  1. using Newtonsoft.Json;
  2. using NPOI.HPSF;
  3. using NPOI.SS.Formula.Functions;
  4. using NPOI.SS.UserModel;
  5. using NPOI.XSSF.UserModel;
  6. using System.Reflection;
  7. namespace Learning.Dotnet
  8. {
  9. public class ExportExcelToDiskUtils<T>
  10. {
  11. public static void ExportToExcel(List<T> list, String FileName)
  12. {
  13. var workbook = new XSSFWorkbook();
  14. var sheet = workbook.CreateSheet("Sheet1");
  15. IRow row1 = sheet.CreateRow(0);
  16. Type t = typeof(T);
  17. int cell = 0;
  18. var properties = t.GetProperties().Where(p => p.GetCustomAttribute<JsonPropertyAttribute>() != null).Select(p => p).ToList();
  19. foreach (var pro in properties)
  20. {
  21. row1.CreateCell(cell).SetCellValue(pro.GetCustomAttribute<JsonPropertyAttribute>().PropertyName);
  22. cell++;
  23. }
  24. int row = 1;
  25. foreach (var item in list)
  26. {
  27. cell = 0;
  28. IRow newRow = sheet.CreateRow(row);
  29. foreach (var pro in properties)
  30. {
  31. var value = list[row - 1];
  32. newRow.CreateCell(cell).SetCellValue(Convert.ToString(pro.GetValue(value, null)));
  33. cell++;
  34. }
  35. row++;
  36. }
  37. // 导出到文件
  38. using (FileStream file = new FileStream(FileName, FileMode.Create, FileAccess.Write))
  39. {
  40. workbook.Write(file);
  41. }
  42. // 释放资源
  43. workbook.Close();
  44. }
  45. }
  46. }

业务对象Model

  1. using Newtonsoft.Json;
  2. namespace Learning.Models.Chinese
  3. {
  4. public class ChineseWord
  5. {
  6. [JsonProperty(PropertyName = "field1")]
  7. public string field1 { get; set; }
  8. [JsonProperty(PropertyName = "field2")]
  9. public string field2 { get; set; }
  10. [JsonProperty(PropertyName = "field3")]
  11. public string field3 { get; set; }
  12. [JsonProperty(PropertyName = "field4")]
  13. public string field4 { get; set; }
  14. [JsonProperty(PropertyName = "field5")]
  15. public string field5 { get; set; }
  16. [JsonProperty(PropertyName = "field6")]
  17. public string field6 { get; set; }
  18. [JsonProperty(PropertyName = "field7")]
  19. public string field7 { get; set; }
  20. [JsonProperty(PropertyName = "field8")]
  21. public string field8 { get; set; }
  22. [JsonProperty(PropertyName = "field9")]
  23. public string field9 { get; set; }
  24. [JsonProperty(PropertyName = "field10")]
  25. public string field10 { get; set; }
  26. }
  27. }

数据封装

这里演示,直接mock数据,实际项目中,替换成自己的数据源即可。

  1. private List<ChineseWord> WrapChineseWord()
  2. {
  3. var chineseWord = new ChineseWord();
  4. chineseWord.field1 = "field1";
  5. chineseWord.field2 = "field2";
  6. chineseWord.field3 = "field3";
  7. chineseWord.field4 = "field4";
  8. chineseWord.field5 = "field5";
  9. chineseWord.field6 = "field6";
  10. chineseWord.field7 = "field7";
  11. chineseWord.field8 = "field8";
  12. chineseWord.field9 = "field9";
  13. chineseWord.field10 = "field10";
  14. var chineseWord2 = new ChineseWord();
  15. chineseWord2.field1 = "field1";
  16. chineseWord2.field2 = "field2";
  17. chineseWord2.field3 = "field3";
  18. chineseWord2.field4 = "field4";
  19. chineseWord2.field5 = "field5";
  20. chineseWord2.field6 = "field6";
  21. chineseWord2.field7 = "field7";
  22. chineseWord2.field8 = "field8";
  23. chineseWord2.field9 = "field9";
  24. chineseWord2.field10 = "field10";
  25. var chineseWordList = new List<ChineseWord>();
  26. chineseWordList.Add(chineseWord);
  27. chineseWordList.Add(chineseWord2);
  28. _Logger.LogInformation("chineseWordList:{}", JsonConvert.SerializeObject(chineseWordList));
  29. return chineseWordList;
  30. }