Skip to main content

Export DataTable to HTML in C#

Introduction

I would like to share a way by which we can export a DataTable to HTML format.

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

First we will bind the DataTable to a DataGridView.

A. Binding DataGridView using DataTable

Procedure
  1. Create a DataTable and define columns as in the following:
    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 as in the following:
    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. Binding DataGridView as in the following:
    1. dataGridView1.DataSource=table;  
  4. Running the code, the following will be the screen.

B. Exporting DataTable to HTML

I have written generic code that creates 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.   
  10. strHTMLBuilder.Append("<tr >");  
  11. foreach (DataColumn myColumn in dt.Columns)  
  12. {  
  13. strHTMLBuilder.Append("<td >");  
  14. strHTMLBuilder.Append(myColumn.ColumnName);  
  15. strHTMLBuilder.Append("</td>");  
  16.   
  17. }  
  18. strHTMLBuilder.Append("</tr>");  
  19.   
  20.   
  21. foreach (DataRow myRow in dt.Rows)  
  22. {  
  23.   
  24. strHTMLBuilder.Append("<tr >");  
  25. foreach (DataColumn myColumn in dt.Columns)  
  26. {  
  27. strHTMLBuilder.Append("<td >");  
  28. strHTMLBuilder.Append(myRow[myColumn.ColumnName].ToString());  
  29. strHTMLBuilder.Append("</td>");  
  30.   
  31. }  
  32. strHTMLBuilder.Append("</tr>");  
  33. }  
  34.   
  35. //Close tags.  
  36. strHTMLBuilder.Append("</table>");  
  37. strHTMLBuilder.Append("</body>");  
  38. strHTMLBuilder.Append("</html>");  
  39.   
  40. string Htmltext = strHTMLBuilder.ToString();  
  41.   
  42. return Htmltext;  
  43.   
  44. }  
c. Understanding the code
  • We created a generic function that uses a DataTable as a parameter.
  • We are using stringbuilder to create dynamic HTML text.
  • Here the output contains an equal number of rows and column as we have in the DataGridView.
  • 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>");  
  6.   
  7. }  
v. Copy Data. The following code creates an equal number of rows as in the DataTable and copies the data to HTML rows.
  1. foreach (DataRow myRow in dt.Rows)  
  2. {  
  3.   
  4. strHTMLBuilder.Append("<tr >");  
  5. foreach (DataColumn myColumn in dt.Columns)  
  6. {  
  7. strHTMLBuilder.Append("<td >");  
  8. strHTMLBuilder.Append(myRow[myColumn.ColumnName].ToString());  
  9. strHTMLBuilder.Append("</td>");  
  10.   
  11. }  
  12. strHTMLBuilder.Append("</tr>");  
  13. }  
d. After executing the code above we would get the following HTML
  1. <html >
    <
    head>
    </
    head>
    <
    body>
    <
    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><tr >
    <
    td >111</td><td >Devesh</td>
    <
    td >Ghaziabad</td></tr>
    <
    tr ><td >222</td><td >ROLI</td>
    <
    td >KANPUR</td></tr><tr >
    <
    td >102</td><td >ROLI</td>
    <
    td >MAINPURI</td></tr><tr >
    <
    td >212</td><td >DEVESH</td>
    <
    td >KANPUR</td></tr></table>
    </
    body>
    </
    html>  
e. Creating HTML file
  1. string HtmlBody = ExportDatatableToHtml(table)  
  2. System.IO.File.WriteAllText(@"c:\abc.HTML", HtmlBody);  
f. Output

Comments

Contact Form

Name

Email *

Message *

Popular posts from this blog

Exporting DataTable to Excel in C# Using Interop

Introduction I would like to share a utility that can be used to export a datatable to Excel file using C#. We have various approaches to perform this activity. In ASP.NET we can do this by changing the Content type to xls but in C# .Net we might have various other approaches. Through this article we will learn how to export a data table to Excel using Interop. Code and Steps Here in this application we will use a Sample datatable and then learn how to export data to an Excel file. We will learn the following things in this article: Creating Excel file using C# Writing data to cells Formatting data to cells Working with Excel range 1. Adding References First we need to add a reference for Microsoft.office.interop.Excel as in the following: 2. Adding sample data table to the code Use the following code to add the sample data table:     static   DataTable  GetTable()     {       ...

Binding CheckBoxList in ASP.Net MVC

Blogspot Introduction I would like to share how to bind the checkbox list in MVC. I will use a Model (a class file) to define various attributes for checkboxes. For a basic understanding of MVC kindly use the following link: ASP.NET MVC Overview The following is the procedure. 1. Creating Model We created a "SubjectModel" class under the Models folder and defined the following two properties: Subject: to display text Selected: to display check/uncheck 2. Creating the controller We created a Controller "BindingCheckBoxController" under the controllers folder We created an Action having the name = "DisplaycheckBoxes" We created a list of Subjects (Subject model class) Returning a list of subjects to the View. public  ActionResult DisplayCheckboxes() {     List<SubjectModel> listsubject =  new  List<SubjectModel>();     listsubject.Add( new  SubjectModel( "Physics" , true )); ...

Indexes In SQL SERVER in Hindi Part 1