1

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.

StackUseR
  • 884
  • 1
  • 11
  • 40

2 Answers2

0

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.

Azamat S
  • 71
  • 1
  • 3
0

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.

StackUseR
  • 884
  • 1
  • 11
  • 40