嗲猫 发表于 2023-2-21 12:48:09

NPOI在EXCEL中追加数据

DataTable dt = (gridControl1.DataSource as DataTable).Copy();
string tempFile = System.IO.Path.GetTempFileName();

string path = AppConfig.GetValue("ImportResult");
System.IO.FileInfo info = new System.IO.FileInfo(path);
info.CopyTo(tempFile, true);
XSSFWorkbook workbook = new XSSFWorkbook(tempFile);
#region 写入杆塔位置验收对比
ISheet sheet = workbook.GetSheet("杆塔位置验收");
for (int i = 0; i < dt.Rows.Count; i++)
{
   // 第二步:创建新数据行
   NPOI.SS.UserModel.IRow row = sheet.CreateRow(sheet.LastRowNum + 1);
   NPOI.SS.UserModel.ICell cell;
   // 添加测试数据
   cell = row.CreateCell(0);
   //cell.CellStyle = cellStyle;
   cell.SetCellValue(dt.Rows["塔号"].ToString());
   cell = row.CreateCell(1);
   //cell.CellStyle = cellStyle;
   cell.SetCellValue(dt.Rows["杆塔型式"].ToString());
   cell = row.CreateCell(2);
   //cell.CellStyle = cellStyle;
   cell.SetCellValue(dt.Rows["档距设计值"].ToString());
   cell = row.CreateCell(3);
   //cell.CellStyle = cellStyle;
   cell.SetCellValue(dt.Rows["档距实测值"].ToString());
   cell = row.CreateCell(4);
   //cell.CellStyle = cellStyle;
   cell.SetCellValue(dt.Rows["档距偏差值"].ToString());
   cell = row.CreateCell(5);
   //cell.CellStyle = cellStyle;
   cell.SetCellValue(dt.Rows["档距允许偏差"].ToString());
   cell = row.CreateCell(6);
   //cell.CellStyle = cellStyle;
   cell.SetCellValue(dt.Rows["线路转角设计值"].ToString());
   cell = row.CreateCell(7);
   //cell.CellStyle = cellStyle;
   cell.SetCellValue(dt.Rows["线路转角实测值"].ToString());
   cell = row.CreateCell(8);
   //cell.CellStyle = cellStyle;
   cell.SetCellValue(dt.Rows["线路转角偏差值"].ToString());
   cell = row.CreateCell(9);
   //cell.CellStyle = cellStyle;
   cell.SetCellValue(dt.Rows["线路转角允许偏差"].ToString());
   cell = row.CreateCell(10);
   //cell.CellStyle = cellStyle;
   cell.SetCellValue(dt.Rows["塔位高程设计值"].ToString());
   cell = row.CreateCell(11);
   //cell.CellStyle = cellStyle;
   cell.SetCellValue(dt.Rows["塔位高程实测值"].ToString());
   cell = row.CreateCell(12);
   //cell.CellStyle = cellStyle;
   cell.SetCellValue(dt.Rows["塔位高程偏差值"].ToString());
   cell = row.CreateCell(13);
   //cell.CellStyle = cellStyle;
      cell.SetCellValue(dt.Rows["塔位高程允许偏差"].ToString());

   cell = row.CreateCell(14);
   //cell.CellStyle = cellStyle;
   cell.SetCellValue(dt.Rows["直线桩位置偏移设计值"].ToString());
   cell = row.CreateCell(15);
   //cell.CellStyle = cellStyle;
   cell.SetCellValue(dt.Rows["直线桩位置偏移实测值"].ToString());
   cell = row.CreateCell(16);
   //cell.CellStyle = cellStyle;
   cell.SetCellValue(dt.Rows["直线桩位置偏移偏差值"].ToString());
   cell = row.CreateCell(17);
   //cell.CellStyle = cellStyle;
   cell.SetCellValue(dt.Rows["直线桩位置偏移允许偏差"].ToString());
   cell = row.CreateCell(18);
   ICellStyle cellStyle = workbook.CreateCellStyle();
   if(dt.Rows["是否合格"].ToString()=="合格")
       cellStyle.FillForegroundColor = HSSFColor.LIGHT_GREEN.index;
   else
       cellStyle.FillForegroundColor = HSSFColor.RED.index;
   cellStyle.FillPattern = FillPatternType.SOLID_FOREGROUND;
   cell.CellStyle = cellStyle;
   cell.SetCellValue(dt.Rows["是否合格"].ToString());
}
#endregion
来源:https://www.cnblogs.com/Caidecheng/archive/2023/02/21/17140581.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: NPOI在EXCEL中追加数据