合并和分组在DataTable中提交的相似文件并打印为HTML格式

时间:2019-05-11 07:27:42

标签: c# datatable

我在C#中有一个DataTAble,如下所示。

my DataTable

我想从上表中生成以下HTML表,该表将如下所示的结果表中的行进行分组和合并。

Result Table

示例代码:

  protected static string ExportDatatableToHtml(DataTable dt)
    {
        StringBuilder strHTMLBuilder = new StringBuilder();
        strHTMLBuilder.Append("<html >");
        strHTMLBuilder.Append("<head>");
        strHTMLBuilder.Append("</head>");
        strHTMLBuilder.Append("<body>");
                    strHTMLBuilder.Append("<table border='2px solid black' cellpadding='1' cellspacing='1' font-size:smaller'>");
        strHTMLBuilder.Append("<tr ><h4>");
        foreach (DataColumn myColumn in dt.Columns)
        {
            strHTMLBuilder.Append("<td >");
            strHTMLBuilder.Append(myColumn.ColumnName);
            strHTMLBuilder.Append("</td>");

        }
        strHTMLBuilder.Append("</h4></tr>");


        foreach (DataRow myRow in dt.Rows)
        {

            strHTMLBuilder.Append("<tr >");
            foreach (DataColumn myColumn in dt.Columns)
            {
                strHTMLBuilder.Append("<td >");

                    if (String.IsNullOrEmpty(myRow[myColumn.ColumnName].ToString())) strHTMLBuilder.Append("<i style='color:Tomato' >  empty  </i>");
                else 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;

    }

0 个答案:

没有答案