[英]EPPlus C# Export data to Excel. Table data overwrites Namebox instead of “Insert” and preserving other cells
I am using EPPlus C# library to generate Excel file.我正在使用 EPPlus C# 库来生成 Excel 文件。 An Excel template is to be filled with SQL Server data that is coming in a DataTable.
Excel 模板将填充来自 DataTable 的 SQL 服务器数据。 Template file uses NameBoxes, the data is filled within those nameboxes.
模板文件使用名称框,数据填充在这些名称框中。 The issue is that for eg.
问题是例如。 I have namebox at position A1
我在 position A1 有名称箱
and another namebox at position B1和 position B1 的另一个名称框
when A1 is filled with a single row data.. it is fine.当 A1 填充单行数据时.. 很好。 But if data in A1 has multple rows it overwrites A2 namebox (namebox at A2 is gone).
但是,如果 A1 中的数据有多行,它会覆盖 A2 名称框(A2 中的名称框已消失)。
The namebox at A2 should be shifted down to A6. A2 处的名称框应下移至 A6。
(Same behaviour we see when doing "Insert" in a cell that shifts other cells ahead instead of overwriting them). (我们在将其他单元格向前移动而不是覆盖它们的单元格中执行“插入”时看到的行为相同)。
And what about the Insert method from Rows collection?那么 Rows 集合中的 Insert 方法呢?
// Insert row at the 4the position.
worksheet.Rows.Insert(3);
I solved it by inserting new rows using the following logic:我通过使用以下逻辑插入新行来解决它:
// Get current cell's address
string oldAddress = nameBox.Start.Address;
// insert new rows in worksheet to preserve other nameboxes
nameBox.Worksheet.InsertRow(nameBox.Start.Row, dt.Rows.Count);
// move to old address
nameBox.Address = oldAddress;
// Fill Name box with Datatable
nameBox.LoadFromDataTable(dt, printHeaders);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.