翼度科技»论坛 编程开发 .net 查看内容

NPOI在EXCEL中追加数据

14

主题

14

帖子

42

积分

新手上路

Rank: 1

积分
42
  1. DataTable dt = (gridControl1.DataSource as DataTable).Copy();
  2. string tempFile = System.IO.Path.GetTempFileName();
  3. string path = AppConfig.GetValue("ImportResult");
  4. System.IO.FileInfo info = new System.IO.FileInfo(path);
  5. info.CopyTo(tempFile, true);
  6. XSSFWorkbook workbook = new XSSFWorkbook(tempFile);
  7. #region 写入杆塔位置验收对比
  8. ISheet sheet = workbook.GetSheet("杆塔位置验收");
  9. for (int i = 0; i < dt.Rows.Count; i++)
  10. {
  11.    // 第二步:创建新数据行
  12.    NPOI.SS.UserModel.IRow row = sheet.CreateRow(sheet.LastRowNum + 1);
  13.    NPOI.SS.UserModel.ICell cell;
  14.    // 添加测试数据
  15.    cell = row.CreateCell(0);
  16.    //cell.CellStyle = cellStyle;
  17.    cell.SetCellValue(dt.Rows[i]["塔号"].ToString());
  18.    cell = row.CreateCell(1);
  19.    //cell.CellStyle = cellStyle;
  20.    cell.SetCellValue(dt.Rows[i]["杆塔型式"].ToString());
  21.    cell = row.CreateCell(2);
  22.    //cell.CellStyle = cellStyle;
  23.    cell.SetCellValue(dt.Rows[i]["档距设计值"].ToString());
  24.    cell = row.CreateCell(3);
  25.    //cell.CellStyle = cellStyle;
  26.    cell.SetCellValue(dt.Rows[i]["档距实测值"].ToString());
  27.    cell = row.CreateCell(4);
  28.    //cell.CellStyle = cellStyle;
  29.    cell.SetCellValue(dt.Rows[i]["档距偏差值"].ToString());
  30.    cell = row.CreateCell(5);
  31.    //cell.CellStyle = cellStyle;
  32.    cell.SetCellValue(dt.Rows[i]["档距允许偏差"].ToString());
  33.    cell = row.CreateCell(6);
  34.    //cell.CellStyle = cellStyle;
  35.    cell.SetCellValue(dt.Rows[i]["线路转角设计值"].ToString());
  36.    cell = row.CreateCell(7);
  37.    //cell.CellStyle = cellStyle;
  38.    cell.SetCellValue(dt.Rows[i]["线路转角实测值"].ToString());
  39.    cell = row.CreateCell(8);
  40.    //cell.CellStyle = cellStyle;
  41.    cell.SetCellValue(dt.Rows[i]["线路转角偏差值"].ToString());
  42.    cell = row.CreateCell(9);
  43.    //cell.CellStyle = cellStyle;
  44.    cell.SetCellValue(dt.Rows[i]["线路转角允许偏差"].ToString());
  45.    cell = row.CreateCell(10);
  46.    //cell.CellStyle = cellStyle;
  47.    cell.SetCellValue(dt.Rows[i]["塔位高程设计值"].ToString());
  48.    cell = row.CreateCell(11);
  49.    //cell.CellStyle = cellStyle;
  50.    cell.SetCellValue(dt.Rows[i]["塔位高程实测值"].ToString());
  51.    cell = row.CreateCell(12);
  52.    //cell.CellStyle = cellStyle;
  53.    cell.SetCellValue(dt.Rows[i]["塔位高程偏差值"].ToString());
  54.    cell = row.CreateCell(13);
  55.    //cell.CellStyle = cellStyle;
  56.         cell.SetCellValue(dt.Rows[i]["塔位高程允许偏差"].ToString());
  57.    cell = row.CreateCell(14);
  58.    //cell.CellStyle = cellStyle;
  59.    cell.SetCellValue(dt.Rows[i]["直线桩位置偏移设计值"].ToString());
  60.    cell = row.CreateCell(15);
  61.    //cell.CellStyle = cellStyle;
  62.    cell.SetCellValue(dt.Rows[i]["直线桩位置偏移实测值"].ToString());
  63.    cell = row.CreateCell(16);
  64.    //cell.CellStyle = cellStyle;
  65.    cell.SetCellValue(dt.Rows[i]["直线桩位置偏移偏差值"].ToString());
  66.    cell = row.CreateCell(17);
  67.    //cell.CellStyle = cellStyle;
  68.    cell.SetCellValue(dt.Rows[i]["直线桩位置偏移允许偏差"].ToString());
  69.    cell = row.CreateCell(18);
  70.    ICellStyle cellStyle = workbook.CreateCellStyle();
  71.    if(dt.Rows[i]["是否合格"].ToString()=="合格")
  72.        cellStyle.FillForegroundColor = HSSFColor.LIGHT_GREEN.index;
  73.    else
  74.        cellStyle.FillForegroundColor = HSSFColor.RED.index;
  75.    cellStyle.FillPattern = FillPatternType.SOLID_FOREGROUND;
  76.    cell.CellStyle = cellStyle;
  77.    cell.SetCellValue(dt.Rows[i]["是否合格"].ToString());
  78. }
  79. #endregion
复制代码
来源:https://www.cnblogs.com/Caidecheng/archive/2023/02/21/17140581.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具