gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 07:28:30 26 4
gpt4 key购买 nike

我正在使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com