asp.net html table to DataTable

添加引用

http://htmlagilitypack.codeplex.com/downloads/get/437941

?





1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

protected
void Export(string
content,string
file)

    {

        HtmlDocument doc = new
HtmlDocument();

        doc.LoadHtml(content);

        HtmlNode table = doc.DocumentNode.SelectSingleNode("//table");

        DataTable dttable = new
DataTable();

        HtmlNode row = table.SelectSingleNode("//tr[@class=\"title\"]");

        string
name = string.Empty;

        foreach
(HtmlNode cell in
row.SelectNodes("th|td"))

        {

            name = cell.InnerText.Replace("\r\n", "").Trim();

            dttable.Columns.Add(name); // create columns from th

        }

        foreach
(var
rows in
doc.DocumentNode.SelectNodes("//tr[td]"))

        {  

            dttable.Rows.Add(rows.SelectNodes("td").Select(td => td.InnerText.Replace("\r\n", "").Trim()).ToArray());

        }

        //return dttable;

        dttable.Rows.RemoveAt(0);

        if
(dttable.Columns.Contains("操作"))

            dttable.Columns.Remove("操作");

        GemBox.ExcelLite.ExcelFile ef = SaveData.DataTableToExcel(dttable);

        Response.ContentType = "application/vnd.ms-excel";

        Response.AddHeader("Content-Disposition", "attachment; filename="
+ file);

        //string path = AppDomain.CurrentDomain.BaseDirectory + "xls_view\\" + file;

        string
path = string.Format("{0}xls_report\\{1}\\{2}", AppDomain.CurrentDomain.BaseDirectory, GetAdmin.cid, GetAdmin.account);

        if
(!System.IO.Directory.Exists(path))

        {

            System.IO.Directory.CreateDirectory(path);

        }

        path = System.IO.Path.Combine(path, file);

        ef.SaveXls(path);

        byte[] data = null; ;

        using
(System.IO.Stream str = new
System.IO.FileStream(path, System.IO.FileMode.Open))

        {

            data = new
byte[str.Length];

            str.Read(data, 0, data.Length);

        }

        this.Response.OutputStream.Write(data, 0, data.Length);

    }

  

asp.net html table to DataTable,码迷,mamicode.com

时间: 2024-10-07 16:48:52

asp.net html table to DataTable的相关文章

DataTables warning: table id=dataTable - Requested unknown parameter 'acceptId' for row 0. For more

重点内容 DataTables warning: table id=dataTable - Requested unknown parameter 'acceptId' for row 0. For more information about this error, please see http://datatables.net/tn/4 使用jquery.datatable时发生上面错误,配置如下: table = $("#dataTable").dataTable({ &quo

DataTables warning: table id=data-table - Requested unknown parameter '3' for row 0.

本文为博主原创,未经允许,不得转载: 在使用jquery 的datatable时,报错在页面弹出弹出框,并提示以下内容: DataTables warning: table id=data-table - Requested unknown parameter '3' for row 0. For more information about this error, please see http://datatables.net/tn/4 错误原因: 在进行列展示的时候,未能解析出对应的值,然

ASP.NET MVC和jQuery DataTable整合

本文包含代码示例说明如何jQuery插件开发者可以集成到ASP.NET MVC应用程序. 下载源代码- 87.4 KB Introduction The jQuery DataTables plug-in is an excellent client-side component that can be used to create rich-functional tables in the web browser. This plug-in adds lot of functionalitie

ASP.NET前台table通过Ajax获取绑定后台查询的json数据

上一篇<ASP.NET前台html页面AJAX提交数据后台ashx页面接收数据>写了前台提交数据后台保存到数据库,数据处理以后用户肯定要查询.接下来就写一个前台table通过ajax  Json 获取值.下面是要实现的效果 每次写博客我都是以一个初学者来看的态度去写,语文也不用合格所有写的有点凌乱.大家看得懂就行,不明白的留言. 1.先来看看前台html页面    查询出来的结果是拼接table上去的 <div class="yjcxdiv"> <p cl

asp.net,C#操作数据库DataTable关于空null的判断

double d=0;if(!Convert.IsDBNull(DataTable.Rows[i][m])){    string str=DataTable.Rows[i][m].ToString().Trim();    if(!String.IsNullOrEmpty(str))  //非空字符串         if(Double.TryParse(str,out d))  //d为正确的数字             d*=0.1; }//此时的d可以赋值给其它了 DBNull:DBNu

Asp.net GridView转换成DataTable

GridView绑定DataTable后,如何获取GridView绑定后显示的值,在项目需求需要的背景下,搜索了获取单元格显示文本的方法,然后写了一个静态方法,经过在项目中的使用,bug的修复,较为稳定. 1 #region ================GridView转DataTable方法================ 2 /// <param name="gv">已绑定数据源的GridView</param> 3 /// <param name

ASP.NET操作DataTable

1.创建 datatable DataTable dt=new Datable();//可以给表创建一个名字,tb 2.给表加个列名: dt.Columns.Add("id", typeof(System.Int32));//类型是可以变换的,比如System.Int32,System.Double.. dt.Columns.Add("type", typeof(System.String)); 3.给表加行,内容: DataRow row=dt.NewRow();

excel转化为table(去掉所有列值都为空的值一行,即返回有效值的DataTable)

/// <summary> /// 去掉所有列值都为空的值一行,即返回有效值的DataTable /// </summary> /// <param name="stream"></param> /// <returns></returns> public static DataTable StreamToDataTableTrimTr(Stream stream) { //第一行一般为标题行. DataTable

ASP.NET操作DataTable各种方法

转:http://www.cnblogs.com/isking/p/6178268.html http://www.cnblogs.com/sntetwt/p/3496477.html public class CreateTable { public static DataTable getTable() { //1.创建 datatable DataTable dt = new DataTable("datatable");//可以给表创建一个名字,datatable /* Dat