Friday, March 11, 2011

Export DataTable to Excel in C#.Net

This is one of the fastest way to export data to Excel file from DataTable

You Need to Import below 2 Libraries for this class to work.
1. Microsoft Office 12.0 Object Library
2. Microsoft Excel 12.0 Object Library


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Threading;
using System.IO;

namespace ExportData2Excel
{
class Export2Excel
{

public void export(DataTable dt)
{
//Create Excel Objects
Microsoft.Office.Interop.Excel.Application oExcel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbooks oBooks;
Microsoft.Office.Interop.Excel.Sheets oSheets;
Microsoft.Office.Interop.Excel.Workbook oBook;
Microsoft.Office.Interop.Excel.Worksheet oSheet;

//Create New Excel WorkBook
oExcel.Visible = true;
oExcel.DisplayAlerts = false;
oExcel.Application.SheetsInNewWorkbook = 1;
oBooks = oExcel.Workbooks;

oBook = (Microsoft.Office.Interop.Excel.Workbook)(oExcel.Workbooks.Add(Type.Missing));
oSheets = oBook.Worksheets;
oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oSheets.get_Item(1);
oSheet.Name = "FinalData";

// Create Array to hold the data of DataTable
object[,] arr = new object[dt.Rows.Count, dt.Columns.Count];

//Fill DataTable in Array
for (int r = 0; r < dt.Rows.Count; r++)
{
DataRow dr = dt.Rows[r];
for (int c = 0; c < dt.Columns.Count; c++)
{
arr[r, c] = dr[c];
}
}

//Set Excel Range to Paste the Data
Microsoft.Office.Interop.Excel.Range c1 = (Microsoft.Office.Interop.Excel.Range)oSheet.Cells[1, 1];
Microsoft.Office.Interop.Excel.Range c2 = (Microsoft.Office.Interop.Excel.Range)oSheet.Cells[1 + dt.Rows.Count - 1, dt.Columns.Count];
Microsoft.Office.Interop.Excel.Range range = oSheet.get_Range(c1, c2);

//Fill Array in Excel
range.Value2 = arr;

}

}

}

16 comments:

  1. Fantastic Solution, Very efficient. 10/10
    works just like a sleek.

    ReplyDelete
  2. what if it contains column as Date Data type

    ReplyDelete
    Replies
    1. It should work fine.... it is just the fact that you might need to format the date as per your need once the data is exported.

      Delete
  3. Superb Solution, I just expoerted 30,000 rows and it did the job within 15-20 seconds, very nice article, I have been looking for this. I just change Object array to String, bcoz obj[,] were automatically removing preceding 0.

    ReplyDelete
  4. You can export datatable to excel using C#/VB.NET with Aspose.Cells for .NET Library also. View the complete code below:

    http://www.aspose.com/docs/display/cellsnet/Exporting%20Data%20from%20Worksheets

    ReplyDelete
  5. Fantastic...Saved hell lot of time...Thanks :-)

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. Thank You! Great work!

    How could we get the Column names into the excel?

    ReplyDelete
  8. In order to get the column names do the following:

    ADD "+1" next to Rows.Count:
    // Create Array to hold the data of DataTable
    object[,] arr = new object[dt.Rows.Count + 1, dt.Columns.Count];

    ADD THIS BEFORE //Fill DataTable...:
    for (int c = 0; c < dt.Columns.Count; c++)
    { arr[0, c] =dt.Columns[c].Caption; }

    ADD "+1" next to r in the deeper for:
    arr[r + 1, c] = dr[c];

    ERASE "-1" in de c2 range. From this:
    ...[1 + dt.Rows.Count - 1, dt.Columns.Count];
    To this
    ...[1 + dt.Rows.Count, dt.Columns.Count];

    ReplyDelete