C# Html格式内容转Csv内容包括table(重点在rowspan和colspan合并),p,div元素

时间:2023-11-29 17:16:02

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:

C# Html格式内容转Csv内容包括table(重点在rowspan和colspan合并),p,div元素

csv:

C# Html格式内容转Csv内容包括table(重点在rowspan和colspan合并),p,div元素

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