---
title: "Export DataTable to HTML in C#.net"  
description: "I would like to share a way by which we can export datatable to HTML format."  
author: "Devesh Omar"  
published: 2014-06-13  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/1414/export-datatable-to-html-in-c-sharp-dot-net  
category: "asp.net"  
tags: ["asp.net"]  
reading_time: 2 minutes  

---

# Export DataTable to HTML in C#.net

##### Introduction

I would like to share a way by which we can export [datatable](https://www.mindstick.com/articles/122/datatable) to HTML format.

This code can be used as [reporting](https://www.mindstick.com/articles/12967/portable-reporting-systems-and-mobile-device-management) [purpose](https://www.mindstick.com/forum/161328/what-is-the-purpose-of-flush-true-in-the-print-function) where sometimes client needs output

data in HTML format.

##### Steps: first we will bind Datatable to datagridview

##### a. Binding DatagridView using Datatable

##### \
Steps:

##### 1. Create datatable and Define Columns

##### ```
 DataTable table = new DataTable(); table.Columns.Add("ID", typeof(int)); table.Columns.Add("NAME", typeof(string));  table.Columns.Add("CITY", typeof(string));   
```

\
2. Add Rows

```
  table.Rows.Add(111, "Devesh", "Ghaziabad");  table.Rows.Add(222, "ROLI", "KANPUR");  table.Rows.Add(102, "ROLI", "MAINPURI");  table.Rows.Add(212, "DEVESH", "KANPUR");
```

3. Binding Datagridview

\

dataGridView1.DataSource=table;

\
4. Running the code following would be screen.

\
![Export DataTable to HTML in C#.net](http://www.mindstick.com/MindStickArticle/2728b439-9158-492c-b3f1-3bf70a094819/Images/image001.png)\

Exporting Datatable to HTML.

\

I have written generic code which could create html text for every datatable.

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

\
Code below:

```
protected string ExportDatatableToHtml(DataTable dt)        {            StringBuilder strHTMLBuilder = new StringBuilder();            strHTMLBuilder.Append("<html >");            strHTMLBuilder.Append("<head>");            strHTMLBuilder.Append("</head>");            strHTMLBuilder.Append("<body>");            strHTMLBuilder.Append("<table border='1px' cellpadding='1' cellspacing='1' bgcolor='lightyellow' style='font-family:Garamond; font-size:smaller'>");                       strHTMLBuilder.Append("<tr >");            foreach (DataColumn myColumn in dt.Columns)            {                strHTMLBuilder.Append("<td >");                strHTMLBuilder.Append(myColumn.ColumnName);                strHTMLBuilder.Append("</td>");             }            strHTMLBuilder.Append("</tr>");                      foreach (DataRow myRow in dt.Rows)            {                 strHTMLBuilder.Append("<tr >");                foreach (DataColumn myColumn in dt.Columns)                {                    strHTMLBuilder.Append("<td >");                    strHTMLBuilder.Append(myRow[myColumn.ColumnName].ToString());                    strHTMLBuilder.Append("</td>");                 }                strHTMLBuilder.Append("</tr>");            }             //Close tags.            strHTMLBuilder.Append("</table>");            strHTMLBuilder.Append("</body>");            strHTMLBuilder.Append("</html>");                   string  Htmltext = strHTMLBuilder.ToString();           return Htmltext;         }
```

3. [Understanding](https://yourviews.mindstick.com/view/88474/why-understanding-yourself-is-so-difficult) the code.

i) We created generic [function](https://www.mindstick.com/articles/43970/purchase-suitable-wedding-dresses-for-your-wedding-function) which take datatable as [parameter](https://www.mindstick.com/blog/450/parameter-class-in-c-sharp)

ii) We are using [stringbuilder](https://www.mindstick.com/blog/99/stringbuilder-class-in-c-sharp) to create [dynamic](https://www.mindstick.com/blog/11080/features-of-java-dynamic-complied-and-interpreted) html text.

iii) Here Output would contains equal number of rows and [column as](https://www.mindstick.com/forum/159334/how-to-set-column-as-auto_increment-in-mysql) we have

in [datagridview](https://www.mindstick.com/articles/63/how-to-add-button-in-datagridview-in-csharp-dot-net).

iv) Creating columns in HTML

```
foreach (DataColumn myColumn in dt.Columns)            {                strHTMLBuilder.Append("<td >");                strHTMLBuilder.Append(myColumn.ColumnName);                strHTMLBuilder.Append("</td>");             }
```

\
v) Copy Data. Following code would create equal number of rows as we have in

datatable and copy it data to html rows.

```
foreach (DataRow myRow in dt.Rows)            {                 strHTMLBuilder.Append("<tr >");                foreach (DataColumn myColumn in dt.Columns)                {                    strHTMLBuilder.Append("<td >");                    strHTMLBuilder.Append(myRow[myColumn.ColumnName].ToString());                    strHTMLBuilder.Append("</td>");                 }                strHTMLBuilder.Append("</tr>");            }
```

d. After executing above code we would get following html

```
<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

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

f. OutPut:

![Export DataTable to HTML in C#.net](http://www.mindstick.com/MindStickArticle/2728b439-9158-492c-b3f1-3bf70a094819/Images/image002.png)**\**

g. Conclusion

We have learned how to create html of datatable.

---

Original Source: https://www.mindstick.com/articles/1414/export-datatable-to-html-in-c-sharp-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
