Creating HTML from a DataTable using C#

后端 未结 9 1440
盖世英雄少女心
盖世英雄少女心 2020-12-30 11:37

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

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

Initially, I th

相关标签:
9条回答
  • 2020-12-30 12:26

    There are a number of ways to output the HTML.

    If this is a relatively easy format (not much formatting, styles etc.) I would definitely go with @mservidio's suggestion.

    If the output is more complex and you have experience with ASP.NET you can go the route of a UserControl which allows more flexibility and management of the output. You can then render the output of the control to HTML like this:

    StringBuilder sb = new StringBuilder();
    StringWriter tw = new StringWriter(sb);
    HtmlTextWriter hw = new HtmlTextWriter(tw);
    
    ctrl.RenderControl(hw);
    return sb.ToString();
    
    0 讨论(0)
  • 2020-12-30 12:27

    Code could be pretty long to write here, I agree with @mservidio. Follow this link to see an example of what you have to do: this link

    0 讨论(0)
  • 2020-12-30 12:29

    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());
    }
    
    0 讨论(0)
  • 2020-12-30 12:30
    public string toHTML_Table(DataTable dt)
            {
                if (dt.Rows.Count == 0)
                    return "";
    
                StringBuilder builder = new StringBuilder();
                builder.Append("<html>");
                builder.Append("<head>");
                builder.Append("<title>");
                builder.Append("Page-");
                builder.Append(Guid.NewGuid().ToString());
                builder.Append("</title>");
                builder.Append("</head>");
                builder.Append("<body>");
                builder.Append("<table border='1px' cellpadding='5' cellspacing='0' ");
                builder.Append("style='border: solid 1px Silver; font-size: x-small;'>");
                builder.Append("<tr align='left' valign='top'>");
                foreach (DataColumn c in dt.Columns)
                {
                    builder.Append("<td align='left' valign='top'><b>");
                    builder.Append(c.ColumnName);
                    builder.Append("</b></td>");
                }
                builder.Append("</tr>");
                foreach (DataRow r in dt.Rows)
                {
                    builder.Append("<tr align='left' valign='top'>");
                    foreach (DataColumn c in dt.Columns)
                    {
                        builder.Append("<td align='left' valign='top'>");
                        builder.Append(r[c.ColumnName]);
                        builder.Append("</td>");
                    }
                    builder.Append("</tr>");
                }
                builder.Append("</table>");
                builder.Append("</body>");
                builder.Append("</html>");
    
                return builder.ToString();
            }
    
    0 讨论(0)
  • 2020-12-30 12:32

    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>");
    
    0 讨论(0)
  • 2020-12-30 12:32

    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.

    0 讨论(0)
提交回复
热议问题