自我写了一组代码来为各样DataTable成立HTML文本

 C. 代码领悟

          //设定BoardID列差别意为空
          BoardID.AllowDBNull = false;

A.
通过DataTable绑定DataGridView

              //表头截至
              htmlStr.Append(“</tr>”);

dataGridView1.DataSource=table;  

    }
}

  1. 创建DataTable,添加列

    DataTable table = new DataTable();
    table.Columns.Add(“ID”, typeof(int));
    table.Columns.Add(“NAME”, typeof(string));
    table.Columns.Add(“CITY”, typeof(string));

          //设定tbBoard表的主键
          tbBoard.PrimaryKey = new DataColumn[] { BoardID };

澳门永利娱乐总站 1

          }

table.Rows.Add(111, "Devesh", "Ghaziabad");  
table.Rows.Add(222, "ROLI", "KANPUR");  
table.Rows.Add(102, "ROLI", "MAINPURI");  
table.Rows.Add(212, "DEVESH", "KANPUR");

        //设定ClassID列差别意为空
        ClassID.AllowDBNull = false;

 3. 绑定DataGridView

        //建立tbClass两列
        DataColumn ClassID = new DataColumn(“ClassID”,
typeof(System.String));
        DataColumn ClassName = new DataColumn(“ClassName”,
typeof(System.String));

protected string ExportDatatableToHtml(DataTable dt)  
{  
StringBuilder strHTMLBuilder = new StringBuilder();  
strHTMLBuilder.Append("<html >");  
strHTMLBuilder.Append("<head>");  
strHTMLBuilder.Append("</head>");  
strHTMLBuilder.Append("<body>");  
strHTMLBuilder.Append("<table border='1px' cellpadding='1' cellspacing='1' bgcolor='lightyellow' style='font-family:Garamond; font-size:smaller'>");  

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

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


foreach (DataRow myRow in dt.Rows)  
{  

strHTMLBuilder.Append("<tr >");  
foreach (DataColumn myColumn in dt.Columns)  
{  
strHTMLBuilder.Append("<td >");  
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;  

}  

        //建立tbBoard的三列
          DataColumn BoardID = new DataColumn(“BoardID”,
typeof(System.String));
          DataColumn BoardName = new DataColumn(“BoardName”,
typeof(System.String));
          DataColumn BoardClassID = new DataColumn(“BoardClassID”,
typeof(System.String));

自身写了一组代码来为各种DataTable创设HTML文本。你能够在你的品种中央直机关接引用。

转自:http://www.cnblogs.com/csdm/archive/2010/02/02/1661808.html

 E. 创建HTML文件

          //DataRow tbClassRow = tbClass.NewRow();
          //for (int i = 1; i <= 5; i++)
          //{
          //    tbClassRow[“ClassID”] = Guid.NewGuid();
          //    tbClassRow[“ClassName”] = string.Format(“分类{0}”,
i);
          //    tbClass.Rows.Add(tbClassRow);
          //}

string HtmlBody = ExportDatatableToHtml(table)  
System.IO.File.WriteAllText(@"c:\abc.HTML", HtmlBody);  

        //先来建立数据库
        DataSet Forum = new DataSet(“Forum”);

明日笔者要向我们大饱眼福一种将DataTable导出为到HTML格式的方法.有时我们须求HTML格式的出口数据,
以下代码就能够扶持我们实现目标,。

              //为行中每一列赋值
              tbBoardRow[“BoardID”] = Guid.NewGuid();
              tbBoardRow[“BoardName”] = string.Format(“版块{0}”, i);
              tbBoardRow[“BoardclassID”] = tbClassRow[“ClassID”];
              //把行参预tbBoard表

 2. 再添加行

 

 F. 运维结果 澳门永利娱乐总站 2

        //把五个数据表参预数据库
        Forum.Tables.Add(tbClass);
        Forum.Tables.Add(tbBoard);

代码如下:

        //把列参预tbClass表
        tbClass.Columns.Add(ClassID);
        tbClass.Columns.Add(ClassName);

<html >
<head>
</head>
<body>
<table border='1px' cellpadding='1' cellspacing='1' bgcolor='lightyellow' style='font-family:Garamond; font-size:smaller'>
<tr >
<td >ID</td>
<td >NAME</td>
<td >CITY</td>
</tr><tr >
<td >111</td><td >Devesh</td>
<td >Ghaziabad</td></tr>
<tr ><td >222</td><td >ROLI</td>
<td >KANPUR</td></tr><tr >
<td >102</td><td >ROLI</td>
<td >MAINPURI</td></tr><tr >
<td >212</td><td >DEVESH</td>
<td >KANPUR</td></tr></table>
</body>
</html>  

        //设定tdClass表的主键
        tbClass.PrimaryKey = new DataColumn[] { ClassID };

先是,大家要绑定DataTable和
DataGridView。

              //把行到场tbClass表
              tbClass.Rows.Add(tbClassRow);
              //实例化tbBoard表的行
              DataRow tbBoardRow = tbBoard.NewRow();

  • 大家创设了二个函数,使用DataTable作为参数。
  • 然后用stringbuilder类成立动态的HTML文本。
  • 出口结果与DataGridView中的行和列数量一样。
  • 在HTML中创造列。

    foreach (DataColumn myColumn in dt.Columns)
    澳门永利娱乐总站,{
    strHTMLBuilder.Append(“

    “);
    strHTMLBuilder.Append(myColumn.ColumnName);
    strHTMLBuilder.Append(“

    “);

    }

  • 复制数据,以下代码就会在DataTable中开创相同数量的行,并将数据复制到HTML行中。

    foreach (DataRow myRow in dt.Rows)
    {

    strHTMLBuilder.Append(“

    “);
    foreach (DataColumn myColumn in dt.Columns)
    {
    strHTMLBuilder.Append(“

    “);
    strHTMLBuilder.Append(myRow[myColumn.ColumnName].ToString());
    strHTMLBuilder.Append(“

    “);

    }
    strHTMLBuilder.Append(“

    “);
    }

              //遍历表中装有行
              for (int i = 0; i < dt.Rows.Count; i++)
              {
                  //数据行开首
                  htmlStr.Append(“<tr>”);
                  //遍历行中列
                  for (int j = 0; j < dt.Columns.Count; j++)
                  {
                      if (!dt.Rows[i].IsNull(j))
                         
htmlStr.Append(string.Format(“<td>{0}</td>”,
dt.Rows[i][j]));
                  }
                  //数据行截止
                  htmlStr.Append(“</tr>”);
              }
              //表结束
              htmlStr.Append(“</table><br>”);
          }
          Response.Write(htmlStr);

 D. 实施上边包车型地铁代码后拿走如下HTML文本

CS:

B. 将DataTable 导出为
HTML**

**

        //再来建立三个数据表
        DataTable tbClass = new DataTable(“tbClass”);
        DataTable tbBoard = new DataTable(“tbBoard”);

 4. 运营结果

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>

          //把列参与tbBoard表
          tbBoard.Columns.Add(BoardID);
          tbBoard.Columns.Add(BoardName);
          tbBoard.Columns.Add(BoardClassID);

              //表头开端
              htmlStr.Append(“<tr
style=’backgound-color:#F0F0F0′>”);
              //遍历表中具有列(使用for遍历)
              for (int i = 0; i < dt.Columns.Count; i++)
              {
                 
htmlStr.Append(string.Format(“<td><strong>{0}</strong></td>”,
dt.Columns[i].ColumnName));
              }

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;

public partial class Dataset : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

          //营造输出字符串
          System.Text.StringBuilder htmlStr = new
System.Text.StringBuilder();
          //遍历Forum中的全部表(使用foreach)遍历
          foreach (DataTable dt in Forum.Tables)
          {
              //表开始
              htmlStr.Append(“<table border=’1′ cellspacing=’0′
cellPadding=’5′ style=’font-size:9pt;font:宋体’>”);

 

              tbBoard.Rows.Add(tbBoardRow);

<html xmlns=”http://www.w3.org/1999/xhtml“>
<head runat=”server”>
    <title>遍历Dataset并出口数据</title>
</head>
<body>
    <form id=”form1″ runat=”server”>
    <div>
    
    </div>
    </form>
</body>
</html>

        // 为多少个表各参预5条记下
          for (int i = 1; i <= 5; i++)
          {
              //实例化tbClass表的行
              DataRow tbClassRow = tbClass.NewRow();
              //为行中每一列赋值
              tbClassRow[“ClassID”] = Guid.NewGuid();
              tbClassRow[“ClassName”] = string.Format(“分类{0}”, 1);

<%@ Page Language=”C#” AutoEventWireup=”true”
CodeFile=”Dataset.aspx.cs” Inherits=”Dataset” %>

相关文章