C# NPOI导出数据到Excel

  1         public void Export()
  2         {
  3             //创建工作簿对象
  4             IWorkbook workbook = new XSSFWorkbook();
  5 
  6             ExportStatistics(workbook);
  7             ExportHumidifyLog(workbook);
  8             ExportHumidifyEventLog(workbook);
  9             ExportHandlingEventLog(workbook);
 10 
 11             //创建流对象并设置存储Excel文件的路径
 12             using (FileStream url = File.OpenWrite(@"D:增湿数据.xlsx"))
 13             {
 14                 //导出Excel文件
 15                 workbook.Write(url);
 16             };
 17         }
 18 
 19         private void ExportStatistics(IWorkbook workbook)
 20         {
 21             //创建工作表
 22             ISheet sheet = workbook.CreateSheet("增湿机与Handling统计");
 23             IRow row0 = sheet.CreateRow(0);
 24             row0.CreateCell(0).SetCellValue("统计项");
 25             row0.CreateCell(1).SetCellValue("循环数");
 26             row0.CreateCell(2).SetCellValue("实际循环时间");
 27             row0.CreateCell(3).SetCellValue("理论循环时间");
 28             row0.CreateCell(4).SetCellValue("运行时间");
 29             row0.CreateCell(5).SetCellValue("总时间");
 30             row0.CreateCell(6).SetCellValue("故障时间");
 31             for (int r = 0; r < Statistics.Count; r++)
 32             {
 33                 //创建行row
 34                 IRow row = sheet.CreateRow(r + 1);
 35                 row.CreateCell(0).SetCellValue(Statistics[r].SourceName);
 36                 row.CreateCell(1).SetCellValue(Statistics[r].CycleCount);
 37                 row.CreateCell(2).SetCellValue(Statistics[r].ActualCycleTime);
 38                 row.CreateCell(3).SetCellValue(Statistics[r].TheoreticalCycleTime);
 39                 row.CreateCell(4).SetCellValue(Statistics[r].UseTime);
 40                 row.CreateCell(5).SetCellValue(Statistics[r].UpTime);
 41                 row.CreateCell(6).SetCellValue(Statistics[r].DownTime);
 42             }
 43         }
 44         private void ExportHumidifyLog(IWorkbook workbook)
 45         {
 46             //创建工作表
 47             ISheet sheet = workbook.CreateSheet("增湿记录");
 48             IRow row0 = sheet.CreateRow(0);
 49             row0.CreateCell(0).SetCellValue("增湿穴");
 50             row0.CreateCell(1).SetCellValue("程序号");
 51             row0.CreateCell(2).SetCellValue("物料名称");
 52             row0.CreateCell(3).SetCellValue("增湿开始时间");
 53             row0.CreateCell(4).SetCellValue("增湿结束时间");
 54             for (int r = 0; r < HumidifyLog.Count; r++)
 55             {
 56                 //创建行row
 57                 IRow row = sheet.CreateRow(r + 1);
 58                 row.CreateCell(0).SetCellValue(HumidifyLog[r].ChamberID);
 59                 row.CreateCell(1).SetCellValue(HumidifyLog[r].HumidifyProgramNo);
 60                 row.CreateCell(2).SetCellValue(HumidifyLog[r].MaterialName);
 61                 row.CreateCell(3).SetCellValue(HumidifyLog[r].HumidifyStartTime);
 62                 row.CreateCell(4).SetCellValue(HumidifyLog[r].HumidifyEndTime);
 63             }
 64         }
 65         private void ExportHumidifyEventLog(IWorkbook workbook)
 66         {
 67             //创建工作表
 68             ISheet sheet = workbook.CreateSheet("增湿机故障统计");
 69             IRow row0 = sheet.CreateRow(0);
 70             row0.CreateCell(0).SetCellValue("故障ID");
 71             row0.CreateCell(1).SetCellValue("故障描述");
 72             row0.CreateCell(2).SetCellValue("故障开始时间");
 73             row0.CreateCell(3).SetCellValue("故障结束时间");
 74             for (int r = 0; r < HumidifyEventLog.Count; r++)
 75             {
 76                 //创建行row
 77                 IRow row = sheet.CreateRow(r + 1);
 78                 row.CreateCell(0).SetCellValue(HumidifyEventLog[r].Code);
 79                 row.CreateCell(1).SetCellValue(HumidifyEventLog[r].Remark);
 80                 row.CreateCell(2).SetCellValue(HumidifyEventLog[r].StartTime);
 81                 row.CreateCell(3).SetCellValue(HumidifyEventLog[r].EndTime);
 82             }
 83         }
 84         private void ExportHandlingEventLog(IWorkbook workbook)
 85         {
 86             //创建工作表
 87             ISheet sheet = workbook.CreateSheet("Handling故障统计");
 88             IRow row0 = sheet.CreateRow(0);
 89             row0.CreateCell(0).SetCellValue("故障ID");
 90             row0.CreateCell(1).SetCellValue("故障描述");
 91             row0.CreateCell(2).SetCellValue("故障开始时间");
 92             row0.CreateCell(3).SetCellValue("故障结束时间");
 93             for (int r = 0; r < HandlingEventLog.Count; r++)
 94             {
 95                 //创建行row
 96                 IRow row = sheet.CreateRow(r + 1);
 97                 row.CreateCell(0).SetCellValue(HandlingEventLog[r].Code);
 98                 row.CreateCell(1).SetCellValue(HandlingEventLog[r].Remark);
 99                 row.CreateCell(2).SetCellValue(HandlingEventLog[r].StartTime);
100                 row.CreateCell(3).SetCellValue(HandlingEventLog[r].EndTime);
101             }
102         }

其中,Statistics,HumidifyLog,HumidifyEventLog,HandlingEventLog为自定义类的列表。

参考:https://www.cnblogs.com/jicheng/p/5961257.html

           https://www.cnblogs.com/zqyw/category/1070314.html

原文地址:https://www.cnblogs.com/zwh1993/p/12222391.html