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("&nbsp;", "").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("&nbsp;", "").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("&nbsp;", "").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("&nbsp;", "").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元素的更多相关文章

  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. [Swift]LeetCode739. 每日温度 | Daily Temperatures

    Given a list of daily temperatures T, return a list such that, for each day in the input, tells you ...

  2. [Swift]LeetCode919. 完全二叉树插入器 | Complete Binary Tree Inserter

    A complete binary tree is a binary tree in which every level, except possibly the last, is completel ...

  3. 阿里云rds数据库迁移实战(多数据源)

    由于某几个业务表数据量太大,数据由业务写,数据部门读. 写压力不大,读却很容易导致长时间等待问题(读由单独系统进行读),导致连接被占用,从而容易并发稍稍增长导致全库卡死! 于是,就拆库呗. 业务系统拆 ...

  4. 使用SCP命令在多个linux系统间进行copy拷贝,上传,下载...

    一,什么是scp scp是linux系统下基于ssh登陆进行安全的远程文件拷贝命令.scp命令可以在linux服务器之间复制文件和目录.scp使用ssh安全协议传输数据,具有和ssh一样的验证机制,从 ...

  5. Hbase篇--Hbase和MapReduce结合Api

    一.前述 Mapreduce可以自定义Inputforma对象和OutPutformat对象,所以原理上Mapreduce可以和任意输入源结合. 二.步骤 将结果写会到hbase中去.  2.1 Ma ...

  6. Python内置函数(2)——all

    英文文档: all(iterable) Return True if all elements of the iterable are true (or if the iterable is empt ...

  7. 4.DOM

    定义 文档对象模型(Document Object Model)是一种用于HTML和XML文档的编程接口. 查找元素 1.直接查找 document.getElementById 根据ID获取一个标签 ...

  8. 智能压缩,摆脱用 Gzip 还是 Brotli 的纠结

    近日,又拍云上线了“智能压缩”功能,同时支持 Gzip 和 Brotli 压缩算法,在节约流量的同时,进一步减少用户的等待时间. CDN 流量问题一直以来是大家关注的重点,又拍云针对流量节约上线了一系 ...

  9. IIS连接数据库:数据库连接出错,请检查连接字串

    搞了一早上,在网上看了各种回答,比如:C盘下的Temp文件夹权限.CONN.asp中的数据库路径问题,都不通.最后发现是: 如果使用的是64位系统,原因有可能是没有64位Access连接驱动. 所以解 ...

  10. SpringBoot入门教程(十五)集成Druid

    Druid是阿里巴巴开源平台上一个数据库连接池实现,它结合了C3P0.DBCP.PROXOOL等DB池的优点,同时加入了日志监控,可以很好的监控DB池连接和SQL的执行情况,可以说是针对监控而生的DB ...