Export DataTable To Excel in C#

In this article, learn how to export a DataTable to an Excel file using C# and ADO.NET.

Introduction

I would like to share a utility that can be used to export a DataTable to an Excel file using C#. There are different ways to export a DataTable to an Excel file. In this article, we will learn how to export a DataTable to Excel using Interop.

Code and Steps

We will learn the following in this article:

  1. Creating Excel file using C#
  2. Writing data to cells
  3. Formatting data to cells
  4. Working with Excel range

1. Add Interop References

First we need to add a reference for Microsoft.office.interop.Excel as in the following:

Adding Refrences

2. Create a DataTable dynamically
 
Use the following code to add a DataTable with data. If you're new to ADO.NET and DataTable, read Mastering DataTable In C#
  1. static DataTable GetTable()  
  2. {  
  3.     //  
  4.     // Here we create a DataTable with four columns.  
  5.     //  
  6.     DataTable table = new DataTable();  
  7.     table.Columns.Add("ID"typeof(int));  
  8.     table.Columns.Add("Name"typeof(string));  
  9.     table.Columns.Add("Sex"typeof(string));  
  10.     table.Columns.Add("CreatedDate"typeof(string));  
  11.     table.Columns.Add("City"typeof(string));  
  12.     //  
  13.     // Here we add five DataRows.  
  14.     //  
  15.     table.Rows.Add(25, "Devesh Omar""M", DateTime.Now, "Noida");  
  16.     table.Rows.Add(50, "Nikhil Vats""M", DateTime.Now, "Noida");  
  17.     table.Rows.Add(10, "Heena Sharma""F", DateTime.Now, "Delhi");  
  18.     table.Rows.Add(21, "Nancy Sharma""F", DateTime.Now, "Delhi");  
  19.     table.Rows.Add(100, "Avinash""M", DateTime.Now, "Delhi");  
  20.     table.Rows.Add(25, "Devesh gupta""M", DateTime.Now, "Delhi");  
  21.     table.Rows.Add(50, "Nikhil gupta""M", DateTime.Now, "Noida");  
  22.     table.Rows.Add(10, "HS gupta""F", DateTime.Now, "Delhi");  
  23.     table.Rows.Add(21, "VS gupta""F", DateTime.Now, "Delhi");  
  24.     table.Rows.Add(100, "RJ gupta""M", DateTime.Now, "Delhi");  
  25.     return table;  
  26. }   
3. Class file for generating Excel

We created a separate class file for generating the Excel (Excelutlity.cs).

4. Creation of Excel objects

Define the following variables:

  1. Microsoft.Office.Interop.Excel.Application excel;   
  2. Microsoft.Office.Interop.Excel.Workbook excelworkBook;  
  3. Microsoft.Office.Interop.Excel.Worksheet excelSheet;  
  4. Microsoft.Office.Interop.Excel.Range excelCellrange;  
I have attached sample code for more details.

5. Initialization of Excel objects 
  1. // Start Excel and get Application object.  
  2. excel = new Microsoft.Office.Interop.Excel.Application();  
  3. // for making Excel visible  
  4. excel.Visible = false;  
  5. excel.DisplayAlerts = false;  
  6. // Creation a new Workbook  
  7. excelworkBook = excel.Workbooks.Add(Type.Missing);  
  8. // Workk sheet  
  9. excelSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelworkBook.ActiveSheet;  
  10. excelSheet.Name = "Test work sheet";  
6. Writing to Excel file
  1. excelSheet.Cells[1, 1] = “Sample test data”;  
  2. excelSheet.Cells[1, 2] = "Date : " + DateTime.Now.ToShortDateString();  
7. Working with range and formatting Excel cells
  1. // now we resize the columns  
  2. excelCellrange = excelSheet.Range[excelSheet.Cells[1, 1], excelSheet.Cells[rowcount, dataTable.Columns.Count]];  
  3. excelCellrange.EntireColumn.AutoFit();  
  4. Microsoft.Office.Interop.Excel.Borders border = excelCellrange.Borders;  
  5. border.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;  
  6. border.Weight = 2d;  
8. Coloring cells

We will use the following function to format and color Excel cells:

  1. public void FormattingExcelCells(Microsoft.Office.Interop.Excel.Range range, string HTMLcolorCode, System.Drawing.Color fontColor, bool IsFontbool)  
  2. {  
  3.     range.Interior.Color = System.Drawing.ColorTranslator.FromHtml(HTMLcolorCode);  
  4.     range.Font.Color = System.Drawing.ColorTranslator.ToOle(fontColor);  
  5.     if (IsFontbool == true)  
  6.     {  
  7.         range.Font.Bold = IsFontbool;  
  8.     }  
  9. }   
9. Build and run the application

We are binding a DataGrid at the load of a Form.

binding datagrid at load of Form

10. The output
 
After clicking on Export to Excel, we will have our Excel file as per the following screen. You need to modify the file out path in the attached code.
 
Export to excel

Conclusion

The attached application can be used in projects for the reporting purposes.