C# Html格式内容转Csv内容包括table(重点在rowspan和colspan合并),p,div元素
Html格式内容转Csv内容,包括table(重点在rowspan和colspan合并),p,div元素,table不能包含嵌套功能。
- /// <summary>
- /// Html格式内容转Csv内容包括table(重点在rowspan和colspan合并),p,div元素
- /// </summary>
- /// <param name="hrml"></param>
- /// <returns></returns>
- private string HtmlToCsv(string hrml)
- {
- HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
- doc.LoadHtml(hrml);
- StringBuilder sbLines = new StringBuilder();
- HtmlAgilityPack.HtmlNodeCollection tList = doc.DocumentNode.SelectNodes("//table");
- if (tList != null)
- {
- foreach (HtmlAgilityPack.HtmlNode table in tList)
- {
- sbLines.AppendLine("#flag_table#,");
- HtmlAgilityPack.HtmlNodeCollection rows = table.SelectNodes("//tr");
- if (rows != null)
- {
- int colCount = ;
- StringBuilder sbTable = new StringBuilder();
- foreach (HtmlAgilityPack.HtmlNode td in rows[].ChildNodes.Where(m => m.OriginalName.ToLower() == "td"))
- {
- HtmlAgilityPack.HtmlAttribute attr = td.Attributes["colspan"];
- int colspan = (attr != null) ? int.Parse(attr.Value) : ;
- colCount = colCount + colspan;
- }
- int rowCount = rows.Count;
- string[][] arr = new string[rowCount][];
- for (int r = ; r < rowCount; r++)
- {
- arr[r] = new string[colCount];
- }
- //填充区域
- for (int r = ; r < rowCount; r++)
- {
- HtmlAgilityPack.HtmlNode tr = rows[r];
- List<HtmlAgilityPack.HtmlNode> cols = tr.ChildNodes.Where(m => m.OriginalName.ToLower() == "td").ToList();
- int colspan = ;
- int rowspan = ;
- for (int c = ; c < cols.Count; c++)
- {
- HtmlAgilityPack.HtmlAttribute cAttr = cols[c].Attributes["colspan"];
- colspan = (cAttr != null) ? int.Parse(cAttr.Value) : ;
- HtmlAgilityPack.HtmlAttribute rAttr = cols[c].Attributes["rowspan"];
- rowspan = (rAttr != null) ? int.Parse(rAttr.Value) : ;
- string text = cols[c].InnerText.Replace(" ", "").Replace(",", ",").Replace("\r", "").Replace("\n", "").Trim();
- if (colspan == && rowspan == )
- {
- continue;
- }
- bool isFirst = true;
- int rFill = r + rowspan;
- for (int ri = r; ri < rFill; ri++)
- {
- int cFill = c + colspan;
- for (int ci = c; ci < cFill; ci++)
- {
- if (isFirst)
- {
- text = (text == string.Empty) ? " " : text;
- arr[ri][ci] = text;
- isFirst = false;
- }
- else
- {
- arr[ri][ci] = string.Empty;
- }
- }
- }
- }
- }
- //填充单元
- for (int r = ; r < rowCount; r++)
- {
- HtmlAgilityPack.HtmlNode tr = rows[r];
- List<HtmlAgilityPack.HtmlNode> cols = tr.ChildNodes.Where(m => m.OriginalName.ToLower() == "td").ToList();
- Queue<string> queue = new Queue<string>();
- for (int c = ; c < cols.Count; c++)
- {
- string text = cols[c].InnerText.Replace(" ", "").Replace(",", ",").Replace("\r", "").Replace("\n", "").Trim();
- queue.Enqueue(text);
- }
- for (int c = ; c < colCount; c++)
- {
- if (arr[r][c] == null)
- {
- string text = queue.Count > ? queue.Dequeue() : string.Empty;
- arr[r][c] = text;
- }
- else
- {
- if (arr[r][c] != string.Empty)
- {
- if (queue.Count > )
- {
- queue.Dequeue();
- }
- }
- }
- }
- }
- //组装成cvs格式内容
- foreach (string[] cols in arr)
- {
- foreach (string col in cols)
- {
- sbLines.Append(col + ",");
- }
- sbLines.AppendLine(",");
- }
- table.RemoveAll();
- }
- }
- }
- HtmlAgilityPack.HtmlNodeCollection pList = doc.DocumentNode.SelectNodes("//p");
- if (pList != null)
- {
- sbLines.AppendLine("#flag_text#,");
- foreach (HtmlAgilityPack.HtmlNode p in pList)
- {
- string text = p.InnerText.Replace(" ", "").Replace(",", ",").Replace("\r", "").Replace("\n", "").Trim();
- text = GetTextByHtml(text);
- if (!string.IsNullOrWhiteSpace(text))
- {
- sbLines.Append(text + ",");
- sbLines.AppendLine(",");
- }
- else
- {
- sbLines.AppendLine(",");
- }
- p.RemoveAll();
- }
- }
- HtmlAgilityPack.HtmlNodeCollection dList = doc.DocumentNode.SelectNodes("//div");
- if (pList != null)
- {
- sbLines.AppendLine("#flag_text#,");
- foreach (HtmlAgilityPack.HtmlNode div in pList)
- {
- string text = div.InnerText.Replace(" ", "").Replace(",", ",").Replace("\r", "").Replace("\n", "").Trim();
- text = GetTextByHtml(text);
- if (!string.IsNullOrWhiteSpace(text))
- {
- sbLines.Append(text + ",");
- sbLines.AppendLine(",");
- }
- else
- {
- sbLines.AppendLine(",");
- }
- //div.RemoveAll();
- }
- }
- return sbLines.ToString();
- }
html:
csv:
url:http://www.cnblogs.com/dreamman/p/5343924.html
C# Html格式内容转Csv内容包括table(重点在rowspan和colspan合并),p,div元素的更多相关文章
- html标签,格式控制标签,内容容器标签,超链接标签,图片标签,表格
打开DREAMWEAVER,新建HTML,如下图: body的属性: bgcolor 页面背景色 background 背景壁纸.图片 text 文字颜色 topmargin 上边距 leftm ...
- python如何转换word格式、读取word内容、转成html
# python如何转换word格式.读取word内容.转成html? import docx from win32com import client as wc # 首先将doc转换成docx wo ...
- 企业架构研究总结(30)——TOGAF架构内容框架之内容元模型(上)
2. 内容元模型(Content Metamodel) 在TOGAF的眼中,企业架构是以一系列架构构建块为基础的,并将目录.矩阵和图形作为其具体展现方式.如果我们把这些表述方式看作为构建块的语法,那么 ...
- TOGAF架构内容框架之内容元模型(上)
TOGAF架构内容框架之内容元模型(上) 2. 内容元模型(Content Metamodel) 在TOGAF的眼中,企业架构是以一系列架构构建块为基础的,并将目录.矩阵和图形作为其具体展现方式.如果 ...
- Django之富文本(获取内容,设置内容)
富文本 1.Rich Text Format(RTF) 微软开发的跨平台文档格式,大多数的文字处理软件都能读取和保存RTF文档,其实就是可以添加样式的文档,和HTML有很多相似的地方 图示 2.tin ...
- 【C#】菜单功能,将剪贴板JSON内容或者xml内容直接粘贴为类
VS 2015菜单功能,将剪贴板JSON内容或者xml内容直接粘贴为类
- content内网,会显示内容,没有内容可地址存在就是这个情况
漏洞地址:http://note.youdao.com/memory/?url=http://www.wooyun.org(如需登录,请注册登录) 正文预览的地方会读取URL地址的<meta n ...
- 为什么当多个inline-block的div中,如果有的div没有内容而有的div有内容,有内容的会下沉?
为什么当多个inline-block的div中,如果有的div没有内容而有的div有内容,有内容的会下沉? 就像这样 两个div高度相同,第二个我写了一个1当作 有内容吧,它就下沉了... 奇怪... ...
- TOGAF架构内容框架之内容元模型(下)
TOGAF架构内容框架之内容元模型(下) 2.2 治理扩展(Governance Extensions) 治理扩展元模型内容 治理扩展部分的意图在于引入额外的,并且与支持运营治理的目标和业务服务相关的 ...
随机推荐
- 调研一类软件的发展演变( 1000-2000 words, in Chinese)
WARING:大量个人观点,可靠性突出一个没有. 随着时代的发展,科技的用途也在发生着改变.最初,计算机是高端科学家用来计算导弹路线.模拟核弹爆炸用的,而现在计算机更多是平凡百姓家的一台娱乐设备.当今 ...
- JavaScript 异步编程的前世今生(下)
ES6 中的 Generator 在 ES6 出现之前,基本都是各式各样类似Promise的解决方案来处理异步操作的代码逻辑,但是 ES6 的Generator却给异步操作又提供了新的思路,马上就有人 ...
- Atlas实现MySQL大表部署读写分离
序章 Atlas是360团队弄出来的一套基于MySQL-Proxy基础之上的代理,修改了MySQL-Proxy的一些BUG,并且优化了很多东西.而且安装方便.配置的注释写的蛮详细的,都是中文.英文不好 ...
- SUSE12Sp3-kafka安装
1.安装java jdk sudo mkdir -p /usr/local/java #创建目录 将jdk-8u201-linux-x64.tar.gz上传到该目录 cd /user/local/ja ...
- MySQL 数据库在 Windows 下修复 only_full_group_by 的错误
本机上新安装了个MySQL数据库,在插入数据的时候一直提示这个错误: [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY ...
- [SQL]LeetCode176. 第二高的薪水 | Second Highest Salary
Write a SQL query to get the second highest salary from the Employee table. +----+--------+ | Id | S ...
- [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 ...
- 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 ...
- MySQL面试必考知识点:揭秘亿级高并发数据库调优与最佳实践法则
做业务,要懂基本的SQL语句: 做性能优化,要懂索引,懂引擎: 做分库分表,要懂主从,懂读写分离... 数据库的使用,是开发人员的基本功,对它掌握越清晰越深入,你能做的事情就越多. 今天我们用10分钟 ...
- ThinkPHP 数据库操作(四) : 聚合查询、时间查询、高级查询
聚合查询 在应用中我们经常会用到一些统计数据,例如当前所有(或者满足某些条件)的用户数.所有用户的最大积分.用户的平均成绩等等,ThinkPHP为这些统计操作提供了一系列的内置方法,包括: 用法示例: ...