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

标签 excel insert row openxml

我正在使用 openxml 创建 Excel 报告。 openxml 使用命名范围对模板 excel 文件进行操作。

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

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

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

问候...

最佳答案

假设您使用的是 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;
    }

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

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

关于excel - openxml - 插入一行,移动其他行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3111152/

相关文章:

python - Pandas 将行除以总数

Excel VBA hlookup 返回 400

excel - 在vba中设置文件路径和文件名

c# - 在 C# 中第 1 行的 'Value1' 列数据被截断

C:将元素插入结构数组

mysql - 如何查找/删除同一行中的重复记录

excel - 初始化时未填充用户窗体组合框

c# - VSTO 备选方案

php - 从 Web 表单插入 PDO

在 R 中读取具有重复行名称的 csv 文件