openxml - 插入一行,移动其他人

bmo*_*yno 5 excel row insert openxml

我使用openxml来创建excel报告.openxml使用命名范围对模板excel文件进​​行操作.

客户端需要行列表末尾的总计行.听起来像是一个合理的要求!!

但是,我从数据库返回的数据表可以包含任意数量的行.使用模板行和'InsertBeforeSelf',我的总计行被覆盖.

我的问题是,使用openxml,我如何在电子表格中插入行,导致每次插入行时总计行向下移动?

问候 ...

小智 7

假设您使用的是SDK 2.0,我使用此函数做了类似的事情:

private static Row CreateRow(Row refRow, SheetData sheetData)
    {
        uint rowIndex = refRow.RowIndex.Value;
        uint newRowIndex;
        var newRow = (Row)refRow.Clone();

        /*IEnumerable<Row> rows = sheetData.Descendants<Row>().Where(r => r.RowIndex.Value >= rowIndex);
        foreach (Row row in rows)
        {
            newRowIndex = System.Convert.ToUInt32(row.RowIndex.Value + 1);

            foreach (Cell cell in row.Elements<Cell>())
            {
                string cellReference = cell.CellReference.Value;
                cell.CellReference = new StringValue(cellReference.Replace(row.RowIndex.Value.ToString(), newRowIndex.ToString()));
            }

            row.RowIndex = new UInt32Value(newRowIndex);
        }*/

        sheetData.InsertBefore(newRow, refRow);
        return newRow;
    }
Run Code Online (Sandbox Code Playgroud)

我不确定你之前是如何使用InsertBeforeSelf做的,所以也许这并没有太大的改进,但这对我有用.我以为你可以用你的总计行作为参考行.(注释掉的部分是为了你的参考行之后有你想要维护的行.我做了一些修改,但它主要来自这个帖子:http://social.msdn.microsoft.com/Forums/en- US/oxmlsdk/thread/65c9ca1c-25d4-482d-8eb3-91a3512bb0ac)

由于它返回新行,您可以使用该对象,然后使用数据库中的数据编辑单元格值.我希望这至少对尝试这样做的人有所帮助......