如何编辑现有电子表格并将新单元格插入每一行

时间:2019-04-18 16:22:34

标签: c# excel openxml-sdk

我正在使用OpenXml SDK编辑现有的电子表格。电子表格具有带有行集的SheetData。我需要在每个现有行中插入新单元格(带有值)。因此,应将特定行的现有单元格移到右侧。

我只是想重复手动命令:

enter image description here

所以我写了这个方法,但是它破坏了文档

        private void InsertNewCell(Row exsistingRow, string value)
        {
            Cell refCell = null;
            foreach (Cell cell in exsistingRow.Elements<Cell>())
            {
                if (string.Compare(cell.CellReference.Value, "A1", true) > 0)
                {
                    refCell = cell;
                    break;
                }
            }

            var newCell = new Cell { CellReference = "A1" };
            exsistingRow.InsertBefore(newCell, refCell);

            newCell.CellValue = new CellValue(value);
        }

什么是正确的解决方案?

0 个答案:

没有答案
相关问题