c# - openxml clonenode方法导致数据在C#中向右移动列
问题描述
我正在编写一个应用程序,它根据特定标准从数据库中获取数据,然后将其导出到 Excel 文件。它完全正常工作,直到它到达 excel 文件的最后一行。这是编写excel文件的代码:
if (recordCounter == currentRecordList.Count() && previousRegionName == regionName)
{
rowInSheet1.Append(
excelController.ConstructCell(regionName, CellValues.String, 2),
excelController.ConstructCell(merchant, CellValues.String, 2),
excelController.ConstructCell(currentRecord.TotalFee.ToString("N2"), CellValues.String, 1),
excelController.ConstructCell(currentRecord.Currency, CellValues.String, 2),
excelController.ConstructCell(currentRecord.DateProcessed.ToString("MMM dd, yyyy"), CellValues.String, 2),
excelController.ConstructCell(description, CellValues.String, 2)
);
sheetData1.Append(rowInSheet1);
rowInSheet1.Append(
excelController.ConstructCell("TOTALS", CellValues.String, 6),
excelController.ConstructCell(" ", CellValues.String, 2),
excelController.ConstructCell(currentRecord.TotalFeeAmount.ToString("N2"), CellValues.String, 6),
excelController.ConstructCell(currentRecord.Currency, CellValues.String, 6),
excelController.ConstructCell(" ", CellValues.String, 2),
excelController.ConstructCell(" ", CellValues.String, 2)
);
sheetData1.Append(rowInSheet1.CloneNode(false));
}
else if (recordCounter == currentRecordList.Count())
{
rowInSheet1.Append(
excelController.ConstructCell(regionName, CellValues.String, 2),
excelController.ConstructCell(merchant, CellValues.String, 2),
excelController.ConstructCell(currentRecord.TotalFee.ToString("N2"), CellValues.String, 1),
excelController.ConstructCell(currentRecord.Currency, CellValues.String, 2),
excelController.ConstructCell(currentRecord.DateProcessed.ToString("MMM dd, yyyy"), CellValues.String, 2),
excelController.ConstructCell(description, CellValues.String, 2)
);
sheetData1.Append(rowInSheet1);
rowInSheet1.Append(
excelController.ConstructCell("TOTALS", CellValues.String, 6),
excelController.ConstructCell(" ", CellValues.String, 2),
excelController.ConstructCell(currentRecord.TotalFeeAmount.ToString("N2"), CellValues.String, 6),
excelController.ConstructCell(currentRecord.Currency, CellValues.String, 6),
excelController.ConstructCell(" ", CellValues.String, 2),
excelController.ConstructCell(" ", CellValues.String, 2)
);
sheetData1.Append(rowInSheet1);
}
else
{
if ((previousCurrency == null && previousRegionName == null) || (previousCurrency == currentCurrency && previousRegionName == regionName))
{
rowInSheet1.Append(
excelController.ConstructCell(regionName, CellValues.String, 2),
excelController.ConstructCell(merchant, CellValues.String, 2),
excelController.ConstructCell(currentRecord.TotalFee.ToString("N2"), CellValues.String, 1),
excelController.ConstructCell(currentRecord.Currency, CellValues.String, 2),
excelController.ConstructCell(currentRecord.DateProcessed.ToString("MMM dd, yyyy"), CellValues.String, 2),
excelController.ConstructCell(description, CellValues.String, 2)
);
sheetData1.Append(rowInSheet1);
previousCurrency = currentCurrency;
previousTotal = currentTotal;
previousRegionName = regionName;
}
else
{
rowInSheet1.Append(
excelController.ConstructCell("TOTALS", CellValues.String, 6),
excelController.ConstructCell(" ", CellValues.String, 2),
excelController.ConstructCell(previousTotal.ToString("N2"), CellValues.String, 6),
excelController.ConstructCell(previousCurrency, CellValues.String, 6),
excelController.ConstructCell(" ", CellValues.String, 2),
excelController.ConstructCell(" ", CellValues.String, 2)
);
sheetData1.Append(rowInSheet1);
Row emptyRow;
emptyRow = new Row();
emptyRow.Append(
excelController.ConstructCell(" ", CellValues.String, 2),
excelController.ConstructCell(" ", CellValues.String, 2),
excelController.ConstructCell(" ", CellValues.String, 2),
excelController.ConstructCell(" ", CellValues.String, 2),
excelController.ConstructCell(" ", CellValues.String, 2),
excelController.ConstructCell(" ", CellValues.String, 2)
);
sheetData1.Append(emptyRow);
Row row;
row = new Row();
row.Append(
excelController.ConstructCell(regionName, CellValues.String, 2),
excelController.ConstructCell(merchant, CellValues.String, 2),
excelController.ConstructCell(currentRecord.TotalFee.ToString("N2"), CellValues.String, 1),
excelController.ConstructCell(currentRecord.Currency, CellValues.String, 2),
excelController.ConstructCell(currentRecord.DateProcessed.ToString("MMM dd, yyyy"), CellValues.String, 2),
excelController.ConstructCell(description, CellValues.String, 2)
);
sheetData1.Append(row);
previousCurrency = currentCurrency;
previousTotal = currentTotal;
previousRegionName = regionName;
}
recordCounter++;
}
}
在第一个 if 语句中使用了克隆节点方法。我故意将其设置为 false,否则它只会在 excel 文件的末尾附加两次。
这是我的预期结果:
这就是我得到的:
我曾尝试使用 Remove 方法,但这似乎也不起作用。
谢谢您的帮助。
解决方案
我通过实例化不同的行来解决这个问题
rowInSheet1 = new Row();
firstRow = new Row();
if (recordCounter == currentRecordList.Count() && previousRegionName == regionName)
{
firstRow.Append(
excelController.ConstructCell(regionName, CellValues.String, 2),
excelController.ConstructCell(merchant, CellValues.String, 2),
excelController.ConstructCell(currentRecord.TotalFee.ToString("N2"), CellValues.String, 1),
excelController.ConstructCell(currentRecord.Currency, CellValues.String, 2),
excelController.ConstructCell(currentRecord.DateProcessed.ToString("MMM dd, yyyy"), CellValues.String, 2),
excelController.ConstructCell(description, CellValues.String, 2)
);
sheetData1.Append(firstRow);
rowInSheet1.Append(
excelController.ConstructCell("TOTALS", CellValues.String, 6),
excelController.ConstructCell(" ", CellValues.String, 2),
excelController.ConstructCell(currentRecord.TotalFeeAmount.ToString("N2"), CellValues.String, 6),
excelController.ConstructCell(currentRecord.Currency, CellValues.String, 6),
excelController.ConstructCell(" ", CellValues.String, 2),
excelController.ConstructCell(" ", CellValues.String, 2)
);
sheetData1.Append(rowInSheet1);
//sheetData1.Append(rowInSheet1.CloneNode(false));
}
我使用了这个 firstRow 而不是使用 clonenode 方法。像魅力一样工作!
推荐阅读
- android - Android - 应用新的 StyleSpan 会删除以前应用的 Span
- docker - 如何在 .netCore 3.1 Ubuntu 容器中使用 PEM 密码/TrustedRoot/TLS Mutual Auth Cert/Private Key
- python - Python 比特币和 pywallet 库从同一个私钥获取不同的 BTC 地址
- python - 浅拷贝列表
- html - 由于其中的内容,表格单元格不具有交互性
- arrays - 如何从一个文件中读取 awk 并将其与另一个文件中的 awk 进行比较
- android - 以编程方式将单元格布局中的约束更改为居中 - Android
- neo4j - neo4j Cypher -- 遍历可变长度路径,但在找到标签时停止
- vb.net - Windows 服务的奇怪问题 - 服务超时
- marklogic - marklogic xquery 如何提交更新,然后可以通过以下行在同一个 xquery 函数中使用更新?