Export DataTable to Excel Using HTML Text in C#

Introduction

We have various ways to export a DataTable to Excel. The following are common ways:

  1. Export to Excel using Interop objects
  2. Export to Excel by changing the content type of the HTTP response (applicable in web only)
  3. Creating CSV or TSV (comma separated or tab separated file )

At this time I want to introduce an alternative way to create an Excel file for a required DataTable.

Let us first discuss some basics of how to export a HTML text to an Excel file.

Please use the following procedure to understand this technique.

1. Create sample HTML file

Code below

  1. <html>  
  2. <table bgcolor='lightgrey' color='Darkblue' border='1px' style="font-family:Calibri">  
  3.     <tr>   
  4.         <td> Col 1 </td>  
  5.         <td> Col 2 </td>  
  6.        <td> Col 3 </td>  
  7.        <td> Col 4 </td>  
  8.        <td> Col 5 </td>  
  9.        <td> Col 6 </td>  
  10. </tr>    
  11. <tr>   
  12.     <td> MANJU GUPTA </td>  
  13.     <td> DEVESH  </td>  
  14.     <td> Manish</td>  
  15.     <td> KB GUPTA </td>  
  16.     <td> MONA</td>  
  17.     <td> Adhyayan </td>  
  18. </tr>  
  19. <tr>   
  20.     <td> ROLI GUPTA </td>  
  21.     <td> ROLI GUPTA </td>  
  22.     <td> ROLI GUPTA</td>  
  23.     <td> ROLI GUPTA </td>  
  24.     <td> MONA</td>  
  25.    <td> Adhyayan </td>  
  26. </tr>  
  27. </table>  
  28. </html> 

Output

Output
Here we can see the HTML in the web browser.

Now we will open this file in Excel.

Excel

The following will be the screen if we open the HTML file in Excel.

html file into excel

Conclusion

Here we created a HTML file and opened it in an Excel file. So the table row and table cell of the HTML file fits into rows and cells of the Excel file with proper formatting and borders and background color and so on.

So we will create HTML text for the data table and then save it as a .xls file, it would work as expected.

We will use the following approach to export the DataTable to Excel.

This code can be used for reporting purposes where sometimes the client needs to output the data in HTML/Excel format.

A. Binding DataGridView using DataTable

Procedure

  1. Create DataTable and Define Columns
    1. DataTable table = new DataTable();  
    2. table.Columns.Add("ID"typeof(int));  
    3. table.Columns.Add("NAME"typeof(string));  
    4. table.Columns.Add("CITY"typeof(string)); 
  2. Add Rows
    1. table.Rows.Add(111, "Devesh""Ghaziabad");  
    2. table.Rows.Add(222, "ROLI""KANPUR");  
    3. table.Rows.Add(102, "ROLI""MAINPURI");  
    4. table.Rows.Add(212, "DEVESH""KANPUR"); 
  3. Bind DataGridView
    1. dataGridView1.DataSource=table; 
  4. Running the code the following will be the screen.

    Running the code

B. Exporting DataTable to HTML.

I have written generic code that could create HTML text for every DataTable.

You can use this code directly in your project for reporting purposes.

Code below

  1. protected string ExportDatatableToHtml(DataTable dt)  
  2. {  
  3.     StringBuilder strHTMLBuilder = new StringBuilder();  
  4.     strHTMLBuilder.Append("<html >");  
  5.     strHTMLBuilder.Append("<head>");  
  6.     strHTMLBuilder.Append("</head>");  
  7.     strHTMLBuilder.Append("<body>");  
  8.     strHTMLBuilder.Append("<table border='1px' cellpadding='1' cellspacing='1' bgcolor='lightyellow' style='font-   family:Garamond; font-size:smaller'>");  
  9.     strHTMLBuilder.Append("<tr >");  
  10.     foreach (DataColumn myColumn in dt.Columns)  
  11.     {  
  12.        strHTMLBuilder.Append("<td >");  
  13.        strHTMLBuilder.Append(myColumn.ColumnName);  
  14.        strHTMLBuilder.Append("</td>");  
  15.     }  
  16.     strHTMLBuilder.Append("</tr>");  
  17.     foreach (DataRow myRow in dt.Rows)  
  18.     {  
  19.        strHTMLBuilder.Append("<tr >");  
  20.        foreach (DataColumn myColumn in dt.Columns)  
  21.        {  
  22.           strHTMLBuilder.Append("<td >");  
  23.           strHTMLBuilder.Append(myRow[myColumn.ColumnName].ToString());  
  24.           strHTMLBuilder.Append("</td>");  
  25.       }  
  26.       strHTMLBuilder.Append("</tr>");  
  27.    }  
  28.    //Close tags.  
  29.    strHTMLBuilder.Append("</table>");  
  30.    strHTMLBuilder.Append("</body>");  
  31.    strHTMLBuilder.Append("</html>");  
  32.    string Htmltext = strHTMLBuilder.ToString();  
  33.    return Htmltext;  

C. Understanding the code

  1. We created a generic function that takes a DataTable as the parameter
  2. We are using StringBuilder to create dynamic HTML text.
  3. Here the output would contain an equal number of rows and columns as we have in the DataGridView.
  4. Creating columns in HTML:
    1. foreach (DataColumn myColumn in dt.Columns)  
    2. {  
    3.     strHTMLBuilder.Append("<td >");  
    4.     strHTMLBuilder.Append(myColumn.ColumnName);  
    5.     strHTMLBuilder.Append("</td>");  
  5. Copy the data. The following code would create an equal number of rows as we have in the DataTable and copy the data to HTML rows.
    1. foreach (DataRow myRow in dt.Rows)  
    2. {  
    3.     strHTMLBuilder.Append("<tr >");  
    4.     foreach (DataColumn myColumn in dt.Columns)  
    5.     {  
    6.         strHTMLBuilder.Append("<td >");  
    7.         strHTMLBuilder.Append(myRow[myColumn.ColumnName].ToString());  
    8.         strHTMLBuilder.Append("</td>");  
    9.     }  
    10.     strHTMLBuilder.Append("</tr>");  

D. Output

After executing the code above we would get the following HTML:

  1. <html>
        <head>
  2.       </head>
  3.         <body>
  4.           <table border='1px' cellpadding='1' cellspacing='1' bgcolor='lightyellow' style='font-          family:Garamond; font-size:smaller'><tr ><td >ID</td><td >NAME</td><td >CITY</td></tr>
  5.         <tr >
  6.           <td >111</td>
  7.           <td >Devesh</td>
  8.           <td >Ghaziabad</td>
  9.        </tr>
  10.        <tr >
  11.           <td >222</td>
  12.           <td >ROLI</td>
  13.           <td >KANPUR</td>
  14.        </tr>
           <tr>
  15.           <td>102</td>
  16.           <td >ROLI</td>
  17.           <td >MAINPURI</td>
  18.       </tr>
  19.       <tr>
  20.         <td >212</td>
  21.         <td >DEVESH</td>
  22.         <td >KANPUR</td>
  23.      </tr>
  24.    </table>
  25.   </body>
  26. </html> 

E. Creating HTML file

string HtmlBody = ExportDatatableToHtml(table)
System.IO.File.WriteAllText(@"c:\abc.xls", HtmlBody);

Important: We are saving the output file in .xls format and we have written HTML text to this Excel file.

HTML Output

Result
Excel Output

 

Conclusion

We have learned how to create an export of a DataTable to Excel using HTML text..

References

Another approach of exporting DataTable to Excel is below:

Exporting DataTable to Excel in C# Using Interop

Next Recommended Readings