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[i]["塔号"].ToString());
   cell = row.CreateCell(1);
   //cell.CellStyle = cellStyle;
   cell.SetCellValue(dt.Rows[i]["杆塔型式"].ToString());
   cell = row.CreateCell(2);
   //cell.CellStyle = cellStyle;
   cell.SetCellValue(dt.Rows[i]["档距设计值"].ToString());
   cell = row.CreateCell(3);
   //cell.CellStyle = cellStyle;
   cell.SetCellValue(dt.Rows[i]["档距实测值"].ToString());
   cell = row.CreateCell(4);
   //cell.CellStyle = cellStyle;
   cell.SetCellValue(dt.Rows[i]["档距偏差值"].ToString());
   cell = row.CreateCell(5);
   //cell.CellStyle = cellStyle;
   cell.SetCellValue(dt.Rows[i]["档距允许偏差"].ToString());
   cell = row.CreateCell(6);
   //cell.CellStyle = cellStyle;
   cell.SetCellValue(dt.Rows[i]["线路转角设计值"].ToString());
   cell = row.CreateCell(7);
   //cell.CellStyle = cellStyle;
   cell.SetCellValue(dt.Rows[i]["线路转角实测值"].ToString());
   cell = row.CreateCell(8);
   //cell.CellStyle = cellStyle;
   cell.SetCellValue(dt.Rows[i]["线路转角偏差值"].ToString());
   cell = row.CreateCell(9);
   //cell.CellStyle = cellStyle;
   cell.SetCellValue(dt.Rows[i]["线路转角允许偏差"].ToString());
   cell = row.CreateCell(10);
   //cell.CellStyle = cellStyle;
   cell.SetCellValue(dt.Rows[i]["塔位高程设计值"].ToString());
   cell = row.CreateCell(11);
   //cell.CellStyle = cellStyle;
   cell.SetCellValue(dt.Rows[i]["塔位高程实测值"].ToString());
   cell = row.CreateCell(12);
   //cell.CellStyle = cellStyle;
   cell.SetCellValue(dt.Rows[i]["塔位高程偏差值"].ToString());
   cell = row.CreateCell(13);
   //cell.CellStyle = cellStyle;
        cell.SetCellValue(dt.Rows[i]["塔位高程允许偏差"].ToString());

   cell = row.CreateCell(14);
   //cell.CellStyle = cellStyle;
   cell.SetCellValue(dt.Rows[i]["直线桩位置偏移设计值"].ToString());
   cell = row.CreateCell(15);
   //cell.CellStyle = cellStyle;
   cell.SetCellValue(dt.Rows[i]["直线桩位置偏移实测值"].ToString());
   cell = row.CreateCell(16);
   //cell.CellStyle = cellStyle;
   cell.SetCellValue(dt.Rows[i]["直线桩位置偏移偏差值"].ToString());
   cell = row.CreateCell(17);
   //cell.CellStyle = cellStyle;
   cell.SetCellValue(dt.Rows[i]["直线桩位置偏移允许偏差"].ToString());
   cell = row.CreateCell(18);
   ICellStyle cellStyle = workbook.CreateCellStyle();
   if(dt.Rows[i]["是否合格"].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[i]["是否合格"].ToString());
}
#endregion
posted @ 2023-02-21 12:49  小蔡1201  阅读(327)  评论(0编辑  收藏  举报