Tuesday, 12 June 2012

Converting a DataTable into HTML Using 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' ");
 htmlBuilder.Append("style='border: solid 1px Black; font-size: small;'>");
 
 //Create Header Row
 htmlBuilder.Append("<tr align='left' valign='top'>");
 
 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;
}

No comments:

Post a Comment