Click here to Skip to main content
15,899,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
public static string ConvertDataTableToHtml(DataTable targetTable)
{
 string htmlString = "";
 
 if (targetTable == null)
 {
  throw new System.ArgumentNullException("targetTable");
 }
  
 StringBuilder htmlBuilder = new StringBuilder();
 
 //Create Top Portion of HTML Document
 htmlBuilder.Append("<html>");
 htmlBuilder.Append("<head>");
 htmlBuilder.Append("<title>");
 htmlBuilder.Append("Page-");
 htmlBuilder.Append(Guid.NewGuid().ToString());
 htmlBuilder.Append("</title>");
 htmlBuilder.Append("</head>");
 htmlBuilder.Append("<body>");
 htmlBuilder.Append("<table border="1px" cellpadding="5" cellspacing="0" hold=" /&gt; htmlBuilder.Append(" style="border: solid 1px Black; font-size: small;">");
 
 //Create Header Row
 htmlBuilder.Append("&lt;tr align="left" valign="top"&gt;");
 
 foreach (DataColumn targetColumn in targetTable.Columns)
 {
  htmlBuilder.Append("<td align="left" valign="top">");
  htmlBuilder.Append(targetColumn.ColumnName);
  htmlBuilder.Append("</td>");
 }
 
 htmlBuilder.Append("</tr>");
 
 //Create Data Rows
 foreach (DataRow myRow in targetTable.Rows)
 {
  htmlBuilder.Append("<tr align="left" valign="top">");
 
  foreach (DataColumn targetColumn in targetTable.Columns)
  {
   htmlBuilder.Append("<td align="left" valign="top">");
   htmlBuilder.Append(myRow[targetColumn.ColumnName].ToString());
   htmlBuilder.Append("</td>");
  }
 
  htmlBuilder.Append("</tr>");
 }
 
 //Create Bottom Portion of HTML Document
 htmlBuilder.Append("</table>");
 htmlBuilder.Append("</body>");
 htmlBuilder.Append("</html>");
 
 //Create String to be Returned
 htmlString = htmlBuilder.ToString();
 
 return htmlString;
}


Using these above code I have already converted a datatable into html format; Now i want to write these html string into a html file and want to display it.

Can anyone suggest me how to do this?
Thanks in advance.
Posted
Updated 17-Apr-12 20:33pm
v2

A html file is just a simply text file with the file extension *.html. So, just write the string to a textfile - simple.
To write the html string to a file see:
http://msdn.microsoft.com/en-us/library/6ka1wd3w.aspx#Y0[^]
To open the file with your prefered web browser:
C#
System.Diagnostics.Process.Start(fileName);
 
Share this answer
 
v2
Comments
sahabiswarup 18-Apr-12 2:36am    
after returning the html string i want to write that string in the html file programatically and then save it and want to display that html file
Use StreamWriter class and write your string to a text file having .htm extension.

For information about using StreamWriter class, visit here[^]
 
Share this answer
 
string filename = "temp.html";
            StreamWriter swXLS = new StreamWriter((MapPath("Logs\\")) + filename);
            swXLS.Write(html.ToString());
            swXLS.Close();
            string attachment = "attachment; filename=" + filename;
            Response.AddHeader("content-disposition", attachment);
            Response.Redirect("~/Officer/Logs/temp.html");
            Response.Write(html.ToString());
            Response.End();
 
Share this answer
 
Comments
nagendrathecoder 18-Apr-12 2:50am    
Your answer is correct but IMO we should not spoonfeed people with readymade code snippets. We can provide them a link from where they can learn it.
nagendrathecoder 18-Apr-12 2:52am    
Oh it was you only who asked the question. :D
Then its ok, ignore my previous comment. :)
sahabiswarup 20-Apr-12 7:16am    
never mind nagendrathecoder
It might help,

HtmlTextWriter[^]
HtmlTextWriter Usage[^]

:)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900