简体   繁体   中英

For Loop of Datatable.rows breaks while exporting Datatable to excel in C# .Net

I am trying to export data from Datatable to Excel . The Datatable has 8540 rows and 31 columns .

The loop below breaks somewhere after 3500 records:

for (int i = 0; i < Tbl.Rows.Count; i++)
{
    for (int j = 0; j < Tbl.Columns.Count; j++)
    {
         workSheet.Cells[(i + 2), (j + 1)] = Tbl.Rows[i][j];
    }
}

Is there any limit of rows while exporting? or something else I am doing wrong? Please Help!

The code I am using for export is:

public static void ExportExcel(this DataTable Tbl, string ExcelFilePath = null)
{
    try
    {
        if (Tbl == null || Tbl.Columns.Count == 0)
            //throw new Exception("ExportToExcel: Null or empty input table!\n");
            Console.WriteLine("ExportToExcel: Null or empty input table!\n");

        // load excel, and create a new workbook
        Excel.Application excelApp = new Excel.Application();
        excelApp.Workbooks.Add();

        // single worksheet
        Excel._Worksheet workSheet = excelApp.ActiveSheet;

        // column headings
        for (int i = 0; i < Tbl.Columns.Count; i++)
        {
            workSheet.Cells[1, (i + 1)] = Tbl.Columns[i].ColumnName;
            workSheet.Cells[1, (i + 1)].Font.Bold = true;
            workSheet.Cells[1, (i + 1)].Font.Size = 12;
        }

        // rows
        for (int i = 0; i < Tbl.Rows.Count; i++)
        {
            // to do: format datetime values before printing
            for (int j = 0; j < Tbl.Columns.Count; j++)
            {
                workSheet.Cells[(i + 2), (j + 1)] = Tbl.Rows[i][j];
            }
        }

        // check fielpath
        if (ExcelFilePath != null && ExcelFilePath != "")
        {
            try
            {
                workSheet.SaveAs(ExcelFilePath);
                excelApp.Quit();
                //MessageBox.Show("Excel file saved!");
            }
            catch (Exception ex)
            {
                //throw new Exception("ExportToExcel: Excel file could not be saved! Check filepath.\n"+ ex.Message);
                Console.WriteLine("ExportToExcel: Excel file could not be saved! Check filepath.\n"+ ex.Message);
            }
        }
        else    // no filepath is given
        {
            excelApp.Visible = true;
        }
    }
    catch (Exception ex)
    {
        //throw new Exception("ExportToExcel: \n" + ex.Message);
        Console.WriteLine("ExportToExcel: \n" + ex.Message); 
    }
}

If I might offer an alternative: Use EPPLUS and its great LoadFromDataTable * Function - this would reduce your code to one line, it is magnitudes faster and it does not rely on Excel directly. Also: the library is free and can be included without licensing problems.

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