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. Jenkins构建集成部署

    一.可运行Jar配置 1. 设置JDK 2. 设置源码 设置构建脚本 #!/bin/bash export BUILD_ID=xxxxxx_content_170 myPath="/data ...

  2. [Swift]LeetCode659. 分割数组为连续子序列 | Split Array into Consecutive Subsequences

    You are given an integer array sorted in ascending order (may contain duplicates), you need to split ...

  3. Linux命令之sftp - 安全文件传输命令行工具

    用途说明 sftp命令可以通过ssh来上传和下载文件,是常用的文件传输工具,它的使用方式与ftp类似,但它使用ssh作为底层传输协议,所以安全性比ftp要好得多. 常用方式 格式:sftp <h ...

  4. Lottie 动画里有图片怎么办?设计师小姐姐也能帮你减少开发量!

    一.序 Hi,大家好,我是承香墨影! Lottie 是 Airbnb 开源的一套跨平台的完整解决方案,设计师只需要使用 After Effectes (之后简称 AE)设计出动画之后,使用 Lotti ...

  5. Android--操作图片Exif信息

    前言 在Android系统中,图片文件在内存中以像素点的二维数组加载,存放像素信息,还会在开头加上一些额外的照片拍摄参数信息,这些信息就是Exif.Android2.0之后,媒体库加入了操作图片Exi ...

  6. JS 中 原生方法 (二) --- 数组 (修---添加ES6新增)

    const arr = [1, 2, 3, 5, 'a', 'b'] /** * * length * 这个只能被 称之为 数组的原生属性, 返回 一个 number * arr.length */ ...

  7. ASP.NET中共有哪几种类型的控件?其中,HTML控件、HTML服务器控件和WEB服务器控件之间有什么区别

    ASP.NET的控件包括WEB服务器控件.WEB用户控件.WEB自定义控件.HTML服务器控件和HTML控件.HTML控件.HTML服务器控件和WEB服务器控件之间的区别如下所示.q      HTM ...

  8. 带着新人学springboot的应用03(springboot+mybatis+缓存 下)

    springboot+mybatis+缓存,基本的用法想必是会了,现在说一说内部大概的原理. 稍微提一下mybatis,只要导入了mybatis的依赖,那么有个自动配置类就会生效,你可以去mybati ...

  9. Vue依赖收集引发的问题

    问题背景 在我们的项目中有一个可视化配置的模块,是通过go.js生成canvas来实现的.但是,我们发现这个模块在浏览器中经常会引起该tab页崩溃.开启chrome的任务管理器一看,进入该页面内存和c ...

  10. ueditor上传图片尺寸过大导致显示难看的解决办法

    昨天遇到这个问题,我也是折腾成了狗, 到处查,最后收集到三个办法,记录一下. 代码贴这里,方便复制 img { max-width: 100%; /*图片自适应宽度*/ } body { overfl ...