Datable转化Table输出

调用:   DataTable dt = new DbHelper(conStr).ToDatatable(sql);
               int[] notShow=new int[dt.Rows.Count];
                this.span.InnerHtml = ConvertDataTableToHTML("头文件名",dt, notShow);

 

        /// <summary>
        /// 输出HtmlTable
        /// </summary>
        /// <param name="headName">表头名</param>
        /// <param name="dt">DataTable</param>
        /// <param name="notShow">行数</param>
        /// <returns></returns>
        public static string ConvertDataTableToHTML(string headName,DataTable dt, int[] notShow)
        {
            StringBuilder htmlTable = new StringBuilder();
            if (dt != null)
            {
                htmlTable.Append("<style>#container{width:1000px;text-align:left;}");
                htmlTable.Append("#uc_box{width:99%;margin:5px;border:1px solid #036;text-align:left;}");
                htmlTable.Append("#uc_forms{margin: 12px;font-size:12px;color:#036;width:100%;}</style>");
                htmlTable.Append("<div id='container'><div id='uc_box'><fieldset><table id='uc_forms'>");
                #region Table Header
                //htmlTable.Append("<tr style='background-color:Azure'><td></td>");
                htmlTable.Append(string.Format("<tr style='text-align:center'> <td colspan='2'>{0}</td></tr>",headName));
                htmlTable.Append("<tr style='background-color:RGB( 75,172,198)'>");
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    if (notShow.Where(o => o.Equals(i)).Count() == 0)
                        htmlTable.Append(string.Format("<td>{0}</td>", dt.Columns[i].ColumnName));
                }
                htmlTable.Append("</tr>");
                #endregion
                #region Table Data
                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        htmlTable.Append("<tr style='background-color:GhostWhite;'>");
                        //htmlTable.Append(string.Format("<td>{0}</td>", i + 1));
                        for (int j = 0; j < dt.Columns.Count; j++)
                        {
                            if (notShow.Where(o => o.Equals(j)).Count() == 0)
                                htmlTable.Append(string.Format("<td>{0}</td>", dt.Rows[i][j].ToString()));
                        }
                        htmlTable.Append("</tr>");
                    }
                }
                else
                    htmlTable.Append(string.Format("<tr style='background-color:GhostWhite'><td colspan='{0}'>No Data Found</td><tr>", dt.Columns.Count + 1 - notShow.Count()));
                #endregion
                htmlTable.Append("</table></fieldset></div></div>");
            }
            return htmlTable.ToString();
        }

你可能感兴趣的:(table)