This project has moved. For the latest updates, please go here.

InsertRowsBelow bug?

Jun 16, 2014 at 2:51 AM
I create a sheet and named 'Settings' and named the cell 'A1:B1' is '_TestMergeClone'.
When I use this like (.xlsm)
workbook.Worksheet("Settings").Range("_TestMergeClone").InsertRowsBelow(10);

there no rows affect.

but when I used like
workbook.Worksheet("Settings").Range("A1").InsertRowsBelow(10);

It works well.

Something i used wrong?



And another question.
eg.
when the first row has many merged cell
and I userd InsertRowsBelow to clone some rows but the clones is not merged,
what can I do to clone the same struct rows under the first row? Thanks。
:)
Coordinator
Jun 18, 2014 at 10:50 PM
Please attach the file because I can't reproduce it. The following works as expected:
var wb = new XLWorkbook();
var ws1 = wb.AddWorksheet("Sheet1");
ws1.Range("A1:C20").Value = "X";
ws1.Range("A1:B1").AddToNamed("Sheet1Named");
ws1.Range("Sheet1Named").InsertRowsBelow(10);

var ws2 = wb.AddWorksheet("Sheet2");
ws2.Range("A1:C20").Value = "X";
ws2.Range("A1:B1").InsertRowsBelow(10);

var ws3 = wb.AddWorksheet("Sheet3");
ws3.Range("A1:C20").Value = "X";
ws3.Range("A1").InsertRowsBelow(10);
About the merging, I try to mimic Excel's behavior as much as possible and that's why you don't get the merged cells with the insert below. You'd have to create the merges manually.
Jun 19, 2014 at 2:27 AM
Edited Jun 19, 2014 at 2:30 AM
Sometimes when write the date to the excel template file and some merges in this maybe cause difference between the closedxml, and I had create a lite demo show it if you have time. :-D

http://www.liuzhiyong2012.cn/dl/ClosedXML_Test.zip

Part Code:(And need download the template)
e.g. A
 using (ClosedXML.Excel.XLWorkbook workbook = new ClosedXML.Excel.XLWorkbook("Test.xlsx"))
            {
                //workbook.Worksheet("Sheet2").Range("A1:B1").InsertRowsBelow(10);
                workbook.Worksheet("Sheet2").Range("_MergedCell").InsertRowsBelow(10);
                workbook.Worksheet("Sheet2").SetTabActive();
                workbook.SaveAs("TestSave.xlsx");
            }
e.g. B
 using (ClosedXML.Excel.XLWorkbook workbook = new ClosedXML.Excel.XLWorkbook("Test.xlsx"))
            {
                workbook.Worksheet("Sheet3").Range("A1:B1").Merge();
                workbook.Worksheet("Sheet3").Range("A1:B1").AddToNamed("_MergedCell2");
                workbook.Worksheet("Sheet3").Range("_MergedCell2").Value = "MergedCellByCode";
                workbook.Worksheet("Sheet3").Range("_MergedCell2").InsertRowsBelow(10);
                workbook.Worksheet("Sheet3").SetTabActive();
                workbook.SaveAs("TestSave2.xlsx");
              }
Coordinator
Jun 19, 2014 at 5:08 PM
It works exactly as it's supposed to work. The named range "_MergedCell" points to the cell "A1" not "A1:B1". So when you call .Range("_MergedCell") it's the same as calling .Range("A1")