I am trying to store some data into an excel file using Spire.Xls.
When copying data from multiple files into the first sheet of another excel file, I want to create a new excel sheet when reaching the row = 1,048,575, and paste the data into this new sheet. Here is my code:
Workbook tempbook = new Workbook();
tempbook.LoadFromFile(PathToSecondFile);
Workbook workbook = new Workbook();
workbook.LoadFromFile(PathToFirstFile);
//import the second workbook's worksheet into the first workbook using a datatable
Worksheet sheet2 = tempbook.Worksheets[0];
//copy data from sheet2 into a datatable
DataTable dataTable = sheet2.ExportDataTable();
//load sheet1
Worksheet sheet1 = workbook.Worksheets[0];
var c1 = sheet1.LastRow;
var c2 = sheet2.LastRow;
if (c1 >= 1048575 || c2 >= 1048575 || (c1 + c2) >= 1048575)
{
//create a new worksheet and append data into it but
//at this line getting Index out of bound exception
Worksheet sheet3 = workbook.Worksheets.Add("NewSheet");
sheet3.InsertDataTable(dataTable, false, sheet3.LastRow + 1, 1);
}
else
{
sheet1.InsertDataTable(dataTable, false, sheet1.LastRow + 1, 1);
}
}
Even if the code surpasses the exception, data is saved into sheet2, but the data to sheet1 is not saved. All suggestions are welcome. Thanks in advance.