Excel 中的 setCellValue 未更新文件

setcellvalue in excel not updating the file

提问人:Joe 提问时间:11/15/2023 最后编辑:hijinxbassistJoe 更新时间:11/15/2023 访问量:16

问:

我正在使用 NPOI,无法让它为我设置值。我使用 1 个流进行读取和写入,但无法将其保存到磁盘中。请帮忙

using (var fs = new FileStream(v, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
{
    IWorkbook wb = new XSSFWorkbook(fs);

    ISheet sheet = wb.GetSheetAt(0);

    if (v1 == "Read")
    {
        if (sheet != null)
        {
            int rowCount = sheet.LastRowNum;

            for (int i = 1; i < rowCount; i++)
            {
                ICell cell = sheet.GetRow(i).GetCell(2);
                customerList.Add(cell.ToString());
            }

            customerList.Distinct().ToList();
            Console.WriteLine("Raeding of Customer List is complete ");
            Console.WriteLine("Number of unique customer is  = {0}", customerList.Count);
        }


        if (sheet != null)
        {
            int rowCount = sheet.LastRowNum;

            for (int i = 1; i < rowCount; i++)
            {
                ICell cell = sheet.GetRow(i).GetCell(7);
                pg.Items.Add(cell.ToString());
            }

            Items.Distinct().ToList();
        }
    }
    if (v1 == "Write")
    {
        if (sheet != null)
        {

            int rowCount = sheet.LastRowNum;

            for (int i = 1; i < rowCount; i++)
            {
                IRow row = sheet.GetRow(i);

                // get the first cell
                ICell cell = row.GetCell(2);
              //  ICell custCell = sheet.GetRow(i).GetCell(2);
                var routeid = items1.Where(p => p.GtmuCustId == cell.ToString()).First().RteId;
                var locationName = items1.Where(p => p.GtmuCustId == cell.ToString()).First().LocNm;
                Console.WriteLine(locationName);
                Console.WriteLine(routeid);

                ICell updatecellRoute = row.CreateCell(11);
                ICell updatecellLocation = row.CreateCell(10);
                //throw new NotImplementedException();
             //   row.CreateCell(10).SetCellValue(routeid);
             //   row.CreateCell(11).SetCellValue(locationName);
                updatecellRoute.SetCellValue(routeid);
                updatecellLocation.SetCellValue(locationName);
            }
            wb.Write(fs);
        }
    }
}
C# .NET Excel NPOI

评论

0赞 Joe 11/15/2023
excel 文件已经有 8 个列,我正在尝试更新第 10 个和 11 个列的一个 .cell。

答: 暂无答案