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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM