简体   繁体   中英

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. An Excel template is to be filled with SQL Server data that is coming in a DataTable. Template file uses NameBoxes, the data is filled within those nameboxes. The issue is that for eg. I have namebox at position A1在此处输入图像描述

and another namebox at position B1

在此处输入图像描述

when A1 is filled with a single row data.. it is fine. But if data in A1 has multple rows it overwrites A2 namebox (namebox at A2 is gone).

在此处输入图像描述

The namebox at A2 should be shifted down to 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?

// 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM