Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating HTML from a DataTable using C#

I need to be able to pass HTML data into Outlook like this:

MailMessage message = new MailMessage();
message.Body = myBody;

Initially, I thought I could pass plain text to it and use PadLeft like this:

somestring.PadLeft(100);

but it did not align everything properly because even though ||||| and MMMMM are both only 5 characters in length, they physically on the screen take up more space.

My solution is to convert data that is in my datatable into an HTML table and then pass it into Outlook.

  1. how do I convert a datatable into an html table?
  2. is there a better solution to my issue?
like image 793
Alex Gordon Avatar asked Mar 20 '12 18:03

Alex Gordon


4 Answers

Loop over your DataTable, and build up the html string. IE:

DataTable dt = new DataTable();

dt.Columns.Add("col1");
dt.Columns.Add("col2");
dt.Columns.Add("col3");
dt.Rows.Add(new object[] { "a", "b", "c" });
dt.Rows.Add(new object[] { "d", "e", "f" });

string tab = "\t";

StringBuilder sb = new StringBuilder();

sb.AppendLine("<html>");
sb.AppendLine(tab + "<body>");
sb.AppendLine(tab + tab + "<table>");

// headers.
sb.Append(tab + tab + tab + "<tr>");

foreach (DataColumn dc in dt.Columns)
{        
    sb.AppendFormat("<td>{0}</td>", dc.ColumnName);        
}

sb.AppendLine("</tr>");

// data rows
foreach (DataRow dr in dt.Rows)
{
    sb.Append(tab + tab + tab + "<tr>");

    foreach (DataColumn dc in dt.Columns)
    {
        string cellValue = dr[dc] != null ? dr[dc].ToString() : "";
        sb.AppendFormat("<td>{0}</td>", cellValue);
    }

    sb.AppendLine("</tr>");
}

sb.AppendLine(tab + tab + "</table>");
sb.AppendLine(tab + "</body>");
sb.AppendLine("</html>");
like image 154
mservidio Avatar answered Nov 06 '22 22:11

mservidio


I just want to share what I did. I hope this would help.

using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;

public void Build(DataSet ds) 
{
    StringWriter sw = new StringWriter();
    HtmlTextWriter w = new HtmlTextWriter(sw);

    foreach (DataTable dt in ds.Tables) 
    {
        //Create a table
        Table tbl = new Table();

        //Create column header row
        TableHeaderRow thr = new TableHeaderRow();
        foreach (DataColumn col in dt.Columns) {
            TableHeaderCell th = new TableHeaderCell();
            th.Text = col.Caption;
            thr.Controls.Add(th);
        }
        tbl.Controls.Add(thr);

        //Create table rows
        foreach (DataRow row in dt.Rows)
        {
            TableRow tr = new TableRow();
            foreach (var value in row.ItemArray)
            {
                TableCell td= new TableCell();
                td.Text = value.ToString();
                tr.Controls.Add(td);
            }
            tbl.Controls.Add(tr);
        }

        tbl.RenderControl(w);

    }

    Response.Write(sw.ToString());
}
like image 25
Andamon A. Abilar Avatar answered Nov 06 '22 23:11

Andamon A. Abilar


how do i convert a datatable into an html table?

The only way is to write code that goes through every row and builds the HTML string the way you need it.

is there a better solution to my issue?

You could use a monospace font (such as Courier) wihch will allow you to align everything properly by simply outputting the right number of spaces but you'd still need to send the email in HTML format setting the proper font on the document.

like image 3
Icarus Avatar answered Nov 06 '22 21:11

Icarus


public string ConvertDataTableToHTMLTableInOneLine(DataTable dt)
    {
        //Convert DataTable To HTML Table in one line
        return "<table>\n<tr>" + string.Join("", dt.Columns.Cast<DataColumn>().Select(dc => "<td>" + dc.ColumnName + "</td>")) + "</tr>\n" +
        "<tr>" + string.Join("</tr>\n<tr>", dt.AsEnumerable().Select(row => "<td>" + string.Join("</td><td>", row.ItemArray) + "</td>").ToArray()) + "</tr>\n<\table>";

    }
like image 3
Dr.sai Avatar answered Nov 06 '22 21:11

Dr.sai