Html格式内容转Csv内容,包括table(重点在rowspan和colspan合并),p,div元素,table不能包含嵌套功能。

  1. /// <summary>
  2. /// Html格式内容转Csv内容包括table(重点在rowspan和colspan合并),p,div元素
  3. /// </summary>
  4. /// <param name="hrml"></param>
  5. /// <returns></returns>
  6. private string HtmlToCsv(string hrml)
  7. {
  8. HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
  9. doc.LoadHtml(hrml);
  10. StringBuilder sbLines = new StringBuilder();
  11. HtmlAgilityPack.HtmlNodeCollection tList = doc.DocumentNode.SelectNodes("//table");
  12. if (tList != null)
  13. {
  14. foreach (HtmlAgilityPack.HtmlNode table in tList)
  15. {
  16. sbLines.AppendLine("#flag_table#,");
  17. HtmlAgilityPack.HtmlNodeCollection rows = table.SelectNodes("//tr");
  18. if (rows != null)
  19. {
  20. int colCount = ;
  21. StringBuilder sbTable = new StringBuilder();
  22. foreach (HtmlAgilityPack.HtmlNode td in rows[].ChildNodes.Where(m => m.OriginalName.ToLower() == "td"))
  23. {
  24. HtmlAgilityPack.HtmlAttribute attr = td.Attributes["colspan"];
  25. int colspan = (attr != null) ? int.Parse(attr.Value) : ;
  26. colCount = colCount + colspan;
  27. }
  28. int rowCount = rows.Count;
  29.  
  30. string[][] arr = new string[rowCount][];
  31. for (int r = ; r < rowCount; r++)
  32. {
  33. arr[r] = new string[colCount];
  34. }
  35.  
  36. //填充区域
  37. for (int r = ; r < rowCount; r++)
  38. {
  39. HtmlAgilityPack.HtmlNode tr = rows[r];
  40. List<HtmlAgilityPack.HtmlNode> cols = tr.ChildNodes.Where(m => m.OriginalName.ToLower() == "td").ToList();
  41.  
  42. int colspan = ;
  43. int rowspan = ;
  44. for (int c = ; c < cols.Count; c++)
  45. {
  46. HtmlAgilityPack.HtmlAttribute cAttr = cols[c].Attributes["colspan"];
  47. colspan = (cAttr != null) ? int.Parse(cAttr.Value) : ;
  48. HtmlAgilityPack.HtmlAttribute rAttr = cols[c].Attributes["rowspan"];
  49. rowspan = (rAttr != null) ? int.Parse(rAttr.Value) : ;
  50. string text = cols[c].InnerText.Replace("&nbsp;", "").Replace(",", ",").Replace("\r", "").Replace("\n", "").Trim();
  51.  
  52. if (colspan == && rowspan == )
  53. {
  54. continue;
  55. }
  56.  
  57. bool isFirst = true;
  58. int rFill = r + rowspan;
  59. for (int ri = r; ri < rFill; ri++)
  60. {
  61. int cFill = c + colspan;
  62. for (int ci = c; ci < cFill; ci++)
  63. {
  64. if (isFirst)
  65. {
  66. text = (text == string.Empty) ? " " : text;
  67. arr[ri][ci] = text;
  68. isFirst = false;
  69. }
  70. else
  71. {
  72. arr[ri][ci] = string.Empty;
  73. }
  74. }
  75. }
  76. }
  77. }
  78.  
  79. //填充单元
  80. for (int r = ; r < rowCount; r++)
  81. {
  82. HtmlAgilityPack.HtmlNode tr = rows[r];
  83. List<HtmlAgilityPack.HtmlNode> cols = tr.ChildNodes.Where(m => m.OriginalName.ToLower() == "td").ToList();
  84. Queue<string> queue = new Queue<string>();
  85. for (int c = ; c < cols.Count; c++)
  86. {
  87. string text = cols[c].InnerText.Replace("&nbsp;", "").Replace(",", ",").Replace("\r", "").Replace("\n", "").Trim();
  88. queue.Enqueue(text);
  89. }
  90. for (int c = ; c < colCount; c++)
  91. {
  92. if (arr[r][c] == null)
  93. {
  94. string text = queue.Count > ? queue.Dequeue() : string.Empty;
  95. arr[r][c] = text;
  96. }
  97. else
  98. {
  99. if (arr[r][c] != string.Empty)
  100. {
  101. if (queue.Count > )
  102. {
  103. queue.Dequeue();
  104. }
  105. }
  106. }
  107. }
  108. }
  109.  
  110. //组装成cvs格式内容
  111. foreach (string[] cols in arr)
  112. {
  113. foreach (string col in cols)
  114. {
  115. sbLines.Append(col + ",");
  116. }
  117. sbLines.AppendLine(",");
  118. }
  119. table.RemoveAll();
  120. }
  121. }
  122. }
  123.  
  124. HtmlAgilityPack.HtmlNodeCollection pList = doc.DocumentNode.SelectNodes("//p");
  125. if (pList != null)
  126. {
  127. sbLines.AppendLine("#flag_text#,");
  128. foreach (HtmlAgilityPack.HtmlNode p in pList)
  129. {
  130. string text = p.InnerText.Replace("&nbsp;", "").Replace(",", ",").Replace("\r", "").Replace("\n", "").Trim();
  131. text = GetTextByHtml(text);
  132. if (!string.IsNullOrWhiteSpace(text))
  133. {
  134. sbLines.Append(text + ",");
  135. sbLines.AppendLine(",");
  136. }
  137. else
  138. {
  139. sbLines.AppendLine(",");
  140. }
  141. p.RemoveAll();
  142. }
  143. }
  144.  
  145. HtmlAgilityPack.HtmlNodeCollection dList = doc.DocumentNode.SelectNodes("//div");
  146. if (pList != null)
  147. {
  148. sbLines.AppendLine("#flag_text#,");
  149. foreach (HtmlAgilityPack.HtmlNode div in pList)
  150. {
  151. string text = div.InnerText.Replace("&nbsp;", "").Replace(",", ",").Replace("\r", "").Replace("\n", "").Trim();
  152. text = GetTextByHtml(text);
  153. if (!string.IsNullOrWhiteSpace(text))
  154. {
  155. sbLines.Append(text + ",");
  156. sbLines.AppendLine(",");
  157. }
  158. else
  159. {
  160. sbLines.AppendLine(",");
  161. }
  162. //div.RemoveAll();
  163. }
  164. }
  165. return sbLines.ToString();
  166. }

html:

csv:

url:http://www.cnblogs.com/dreamman/p/5343924.html

C# Html格式内容转Csv内容包括table(重点在rowspan和colspan合并),p,div元素的更多相关文章

  1. html标签,格式控制标签,内容容器标签,超链接标签,图片标签,表格

    打开DREAMWEAVER,新建HTML,如下图: body的属性: bgcolor 页面背景色 background  背景壁纸.图片 text  文字颜色 topmargin  上边距 leftm ...

  2. python如何转换word格式、读取word内容、转成html

    # python如何转换word格式.读取word内容.转成html? import docx from win32com import client as wc # 首先将doc转换成docx wo ...

  3. 企业架构研究总结(30)——TOGAF架构内容框架之内容元模型(上)

    2. 内容元模型(Content Metamodel) 在TOGAF的眼中,企业架构是以一系列架构构建块为基础的,并将目录.矩阵和图形作为其具体展现方式.如果我们把这些表述方式看作为构建块的语法,那么 ...

  4. TOGAF架构内容框架之内容元模型(上)

    TOGAF架构内容框架之内容元模型(上) 2. 内容元模型(Content Metamodel) 在TOGAF的眼中,企业架构是以一系列架构构建块为基础的,并将目录.矩阵和图形作为其具体展现方式.如果 ...

  5. Django之富文本(获取内容,设置内容)

    富文本 1.Rich Text Format(RTF) 微软开发的跨平台文档格式,大多数的文字处理软件都能读取和保存RTF文档,其实就是可以添加样式的文档,和HTML有很多相似的地方 图示 2.tin ...

  6. 【C#】菜单功能,将剪贴板JSON内容或者xml内容直接粘贴为类

    VS 2015菜单功能,将剪贴板JSON内容或者xml内容直接粘贴为类

  7. content内网,会显示内容,没有内容可地址存在就是这个情况

    漏洞地址:http://note.youdao.com/memory/?url=http://www.wooyun.org(如需登录,请注册登录) 正文预览的地方会读取URL地址的<meta n ...

  8. 为什么当多个inline-block的div中,如果有的div没有内容而有的div有内容,有内容的会下沉?

    为什么当多个inline-block的div中,如果有的div没有内容而有的div有内容,有内容的会下沉? 就像这样 两个div高度相同,第二个我写了一个1当作 有内容吧,它就下沉了... 奇怪... ...

  9. TOGAF架构内容框架之内容元模型(下)

    TOGAF架构内容框架之内容元模型(下) 2.2 治理扩展(Governance Extensions) 治理扩展元模型内容 治理扩展部分的意图在于引入额外的,并且与支持运营治理的目标和业务服务相关的 ...

随机推荐

  1. 调研一类软件的发展演变( 1000-2000 words, in Chinese)

    WARING:大量个人观点,可靠性突出一个没有. 随着时代的发展,科技的用途也在发生着改变.最初,计算机是高端科学家用来计算导弹路线.模拟核弹爆炸用的,而现在计算机更多是平凡百姓家的一台娱乐设备.当今 ...

  2. JavaScript 异步编程的前世今生(下)

    ES6 中的 Generator 在 ES6 出现之前,基本都是各式各样类似Promise的解决方案来处理异步操作的代码逻辑,但是 ES6 的Generator却给异步操作又提供了新的思路,马上就有人 ...

  3. Atlas实现MySQL大表部署读写分离

    序章 Atlas是360团队弄出来的一套基于MySQL-Proxy基础之上的代理,修改了MySQL-Proxy的一些BUG,并且优化了很多东西.而且安装方便.配置的注释写的蛮详细的,都是中文.英文不好 ...

  4. SUSE12Sp3-kafka安装

    1.安装java jdk sudo mkdir -p /usr/local/java #创建目录 将jdk-8u201-linux-x64.tar.gz上传到该目录 cd /user/local/ja ...

  5. MySQL 数据库在 Windows 下修复 only_full_group_by 的错误

    本机上新安装了个MySQL数据库,在插入数据的时候一直提示这个错误: [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY ...

  6. [SQL]LeetCode176. 第二高的薪水 | Second Highest Salary

    Write a SQL query to get the second highest salary from the Employee table. +----+--------+ | Id | S ...

  7. [Swift]LeetCode747. 至少是其他数字两倍的最大数 | Largest Number At Least Twice of Others

    In a given integer array nums, there is always exactly one largest element. Find whether the largest ...

  8. Android device debug (adb) by Charge Only mode

    Android device debug by Charge Only mode Method 1 Connect devices to computer and execute lsusb Find ...

  9. MySQL面试必考知识点:揭秘亿级高并发数据库调优与最佳实践法则

    做业务,要懂基本的SQL语句: 做性能优化,要懂索引,懂引擎: 做分库分表,要懂主从,懂读写分离... 数据库的使用,是开发人员的基本功,对它掌握越清晰越深入,你能做的事情就越多. 今天我们用10分钟 ...

  10. ThinkPHP 数据库操作(四) : 聚合查询、时间查询、高级查询

    聚合查询 在应用中我们经常会用到一些统计数据,例如当前所有(或者满足某些条件)的用户数.所有用户的最大积分.用户的平均成绩等等,ThinkPHP为这些统计操作提供了一系列的内置方法,包括: 用法示例: ...