更新单元格值会破坏行样式。 NPOI,C#

Updating a cell value breaks row style. NPOI, C#

晚上好,最近我尝试使用 NPOI 库 (C#) 更新 .xls 文件中的单元格值,但是,当我使用 cell.SetCellValue("anyvalue");

执行此操作时

我只能在某些单元格中看到变化。其他单元格只是空的。

试图保存单元格的样式并使用 cell.CellStyle 重写它,但还是一样。

一般来说,我得到的值只有一半需要填写的地方。

使用该代码,其中 nameAndValues[0] 包含单元格名称,nameAndValues[1] 包含其值。

using (FileStream rstr = new FileStream(currentPath + $"/{excelName}", FileMode.Open, FileAccess.Read))
                {
                    var workbook = new HSSFWorkbook(rstr);
                    var sheet = workbook.GetSheetAt(0);
                    using (FileStream wstr = new FileStream(currentPath + $"/{excelName}", FileMode.Open, FileAccess.Write))
                    {
                        for (int i = 0; i < values.Count; i++)
                        {
                            var cr = new CellReference(namesAndValue[i, 0]);
                            var row = sheet.CreateRow(cr.Row);
                            var cell = row.CreateCell(cr.Col);
                            cell.SetCellValue(namesAndValue[i, 1]);

                        }
                        workbook.Write(wstr);
                        wstr.Close();
                    }
                    rstr.Close();
                }

当您调用 sheet.CreateRow(0) 时,sheet 的第一行将被擦除并插入一个没有样式的空行。 row.CreateCell().

也是如此

因此您一遍又一遍地调用 CreateRow,只使该行的最后一个值保留下来。

我认为这可能是问题所在。