You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
2.7 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.Text;
//using HtmlAgilityPack;
using System.Data;
namespace Estsh.Core.Util
{
public class HtmlTableParser
{
/// <summary>
/// 从 HTML 文本中解析 <table></table> 的内容
/// </summary>
/// <param name="htmlContext">HTML文本</param>
/// <param name="tableHeadIndex">表头在Table中的行数从 0 开始</param>
/// <returns></returns>
public static DataSet ParseHtmlTable(string htmlContext, int tableHeadIndex)
{
DataSet ds = new DataSet();
// 表头在第几行
int RowHeadIndex = tableHeadIndex;
// 创建 HTML DOM 树对象
//HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
// 加载 HTML 文本
//doc.LoadHtml(htmlContext);
// 当前行计数器
int RowCount = 0;
// 当前列计数器
int CellCount = 0;
// 遍历 DOM 树中的所有 TABLE
//foreach (HtmlNode table in doc.DocumentNode.SelectNodes("//table"))
//{
// DataTable dt = new DataTable(table.Id.ToString());
// RowCount = 0;
// // 遍历行
// foreach (HtmlNode row in table.SelectNodes("tr"))
// {
// DataRow dr = dt.NewRow();
// CellCount = 0;
// // 遍历列
// foreach (HtmlNode cell in row.SelectNodes("th|td"))
// {
// string CellValue = cell.InnerText.Replace("&nbsp;", "");
// // 创建表头
// if (RowCount == RowHeadIndex)
// {
// DataColumn dc = new DataColumn(CellValue, typeof(string));
// dt.Columns.Add(dc);
// }
// // 读取表格内容
// else if (RowCount > RowHeadIndex)
// {
// dr[CellCount++] = CellValue;
// }
// else
// {
// // 略过表头之前的内容
// }
// }
// // 加入数据行
// if (RowCount > RowHeadIndex)
// {
// dt.Rows.Add(dr);
// }
// RowCount++;
// }
// 把数据表加入到数据集
// ds.Tables.Add(dt);
//}
return ds;
}
}
}