简体   繁体   中英

Add new sheet to Excel workbook to store data when row limit > 1M

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.

Is it possoble to switch to Excel 2007+ format? In this way you can find a lot of providers for excel, for example Apache npoi, Spreadsheet Light.

It was pretty simple actually... A bit workaround did the task. So here's the code:

int c1 = workbook.ActiveSheet.LastRow, c2 = tempbook.Worksheets[0].LastRow;

if ((c1 + c2) <= 1048575)
{
    //import the second workbook's worksheet into the first workbook using a datatable
    //load 1st sheet of tempbook into sheet
    Worksheet sheet = tempbook.Worksheets[0];
    //copy data from sheet into a datatable
    DataTable dataTable = sheet.ExportDataTable();
    //load sheet1
    Worksheet sheet1 = workbook.Worksheets[workbook.ActiveSheetIndex];
    sheet1.InsertDataTable(dataTable, false, sheet1.LastRow + 1, 1);
}
else if ((c1 >= 1048575 && c2 >= 1048575) || c1 >= 1048575 || c2 >= 1048575 || (c1 + c2) >= 1048575)
{
    workbook.Worksheets.AddCopy(tempbook.Worksheets[0]);
    indx = workbook.ActiveSheet.Index;
    workbook.ActiveSheetIndex = ++indx;
}
else
{
    //import the second workbook's worksheet into the first workbook using a datatable
    //load 1st sheet of tempbook into sheet
    Worksheet sheet = tempbook.Worksheets[0];
    //copy data from sheet into a datatable
    DataTable dataTable = sheet.ExportDataTable();
    //load sheet1
    Worksheet sheet1 = workbook.Worksheets[workbook.ActiveSheetIndex];
    sheet1.InsertDataTable(dataTable, false, sheet1.LastRow + 1, 1);
}

Obviously, the if and else block code is same. But couldn't think of anything else. Any modification are welcome.

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