Skip to content

Commit c869f60

Browse files
authored
Merge pull request #1836 from EPPlusSoftware/bug/i1835
#1835 - Insert row in table caused corrupt workbook
2 parents 5358567 + 90a6628 commit c869f60

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/EPPlus/Table/ExcelTable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1142,7 +1142,7 @@ public ExcelRangeBase InsertRow(int position, int rows=1, bool copyStyles=true)
11421142

11431143
if (_address._fromRow > firstRow)
11441144
{
1145-
_address = new ExcelAddressBase(firstRow, _address._fromCol, _address._toRow, _address._toCol, _address._fromRowFixed, _address._fromColFixed, _address._toRowFixed, _address._toColFixed, _address.WorkSheetName, null);
1145+
Address = new ExcelAddressBase(firstRow, _address._fromCol, _address._toRow, _address._toCol, _address._fromRowFixed, _address._fromColFixed, _address._toRowFixed, _address._toColFixed, _address.WorkSheetName, null);
11461146
}
11471147

11481148
return range;

src/EPPlusTest/Issues/TableIssues.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using System.IO;
44
using OfficeOpenXml.FormulaParsing;
55
using System;
6+
using System.Collections.Generic;
7+
using System.Data;
68

79
namespace EPPlusTest.Issues
810
{
@@ -52,5 +54,33 @@ public void i1642()
5254
SaveAndCleanup(package);
5355
}
5456
}
57+
58+
[TestMethod]
59+
public void sc813()
60+
{
61+
62+
var dataTable = new DataTable();
63+
64+
dataTable.Columns.Add("A", typeof(string));
65+
dataTable.Columns.Add("B", typeof(string));
66+
dataTable.Columns.Add("C", typeof(string));
67+
dataTable.Columns.Add("D", typeof(string));
68+
dataTable.Columns.Add("E", typeof(string));
69+
70+
using var package = OpenPackage("sc813.xlsx", true);
71+
var worksheet = package.Workbook.Worksheets.Add("TestSheet");
72+
var range = worksheet.Cells["A2"].LoadFromDataTable(dataTable, true);
73+
var table = worksheet.Tables.Add(range, "TestTable");
74+
table.ShowHeader = true;
75+
76+
//Initial issue: Commenting either of these insert/load combos will result in a corrupted workbook
77+
table.InsertRow(int.MaxValue, 5);
78+
worksheet.Cells[table.Address.End.Row, table.Address.Start.Column].LoadFromArrays(new List<object[]> { new[] { "1", "2", "3", "4", "5" } });
79+
table.InsertRow(int.MaxValue, 5);
80+
worksheet.Cells[table.Address.End.Row, table.Address.Start.Column].LoadFromArrays(new List<object[]> { new[] { "z", "x", "y", "x", "w" } });
81+
82+
83+
SaveAndCleanup(package);
84+
}
5585
}
5686
}

0 commit comments

Comments
 (0)