在NPOI中的现有工作表中插入单元格

时间:2014-03-28 08:41:37

标签: c# asp.net-mvc npoi

请有人帮帮我。我需要在现有工作表中插入一个单元格。我正在使用NPOI库来处理Excel数据。 感谢。

这就是我想要实现的目标。我在Sheet对象中加载了一个excel。我的目标是在现有工作表中插入几列并将其保存到另一个excel文件中。我试图在索引上创建一个单元格,我打算创建单元格,它会覆盖该位置的前一个单元格。我需要一个功能区域,我可以在两个单元格之间插入一个单元格。 以下是代码。

 using (FileStream fs = new FileStream(Path, FileMode.Open, FileAccess.ReadWrite))
 {
            wb = new HSSFWorkbook(fs);
            NPOI.SS.UserModel.ISheet sheet = wb.GetSheetAt(0);
            NPOI.SS.UserModel.IRow headerRow = sheet.GetRow(1);
            int columnCount = headerRow.Cells.Count;
            int rowCount = sheet.LastRowNum;
            string ColumnContractStartDate = string.Empty;

            //adding header cells at index 7 and 8
            HSSFCell cell7 = (HSSFCell) headerRow.CreateCell(7);
            cell7.SetCellValue("* Contract Start Date");

            HSSFCell cell8 = (HSSFCell) headerRow.CreateCell(8);
            cell8.SetCellValue("* Contract End Date");

              //adding row cells at index 7 and 8
                for (int j = 2; j < rowCount; j++)
                {
                    NPOI.SS.UserModel.IRow dataRow = sheet.GetRow(j);
                    //PFS ROws
                    dataRow.CreateCell(7).SetCellValue("");
                    dataRow.CreateCell(8).SetCellValue("");
                 }

                FileStream file = new FileStream("F:\\ShareDataFeed\\test12.xls", FileMode.Create);
             wb.Write(file);
             file.Close();
             fs.Close();
   }

0 个答案:

没有答案