npoi 读取 html,将HtmlTable内容导出到Excel,使用NPOI组件

///

/// 将DataTable导出到Excel

///

/// html表格内容

/// 仅文件名(非路径)

/// 返回Excel文件绝对路径

public static string ExportHtmlTableToExcel(string htmlTable, string fileName)

{

string result;

try

{

#region 第一步:将HtmlTable转换为DataTable

htmlTable = htmlTable.Replace("\"", "'");

var trReg = new Regex(pattern: @"(?<=())");

var trMatchCollection = trReg.Matches(htmlTable);

DataTable dt = new DataTable("data");

for (int i = 0; i < trMatchCollection.Count; i++)

{

var row = "

";

var tdReg = new Regex(pattern: @"(?<=())");

var tdMatchCollection = tdReg.Matches(row);

if (i == 0)

{

foreach (var rd in tdMatchCollection)

{

var tdValue = RemoveHtml("

");

DataColumn dc = new DataColumn(tdValue);

dt.Columns.Add(dc);

}

}

if (i > 0)

{

DataRow dr = dt.NewRow();

for (int j = 0; j < tdMatchCollection.Count; j++)

{

var tdValue = RemoveHtml("

");

dr[j] = tdValue;

}

dt.Rows.Add(dr);

}

}

#endregion

#region 第二步:将DataTable导出到Excel

result = "ok_" + ExportDataSetToExcel(dt, fileName);

#endregion

}

catch (Exception ex)

{

result = "err_" + ex.Message;

}

return result;

}

///

/// 将DataTable导出到Excel

///

/// DataTable

/// 仅文件名(非路径)

/// 返回Excel文件绝对路径

public static string ExportDataSetToExcel(DataTable dt, string fileName)

{

#region 表头

HSSFWorkbook hssfworkbook = new HSSFWorkbook();

NPOI.SS.UserModel.Sheet hssfSheet = hssfworkbook.CreateSheet(fileName);

hssfSheet.DefaultColumnWidth = 13;

hssfSheet.SetColumnWidth(0, 25 * 256);

hssfSheet.SetColumnWidth(3, 20 * 256);

// 表头

NPOI.SS.UserModel.Row tagRow = hssfSheet.CreateRow(0);

tagRow.Height = 22 * 20;

// 标题样式

NPOI.SS.UserModel.CellStyle cellStyle = hssfworkbook.CreateCellStyle();

cellStyle.Alignment = HorizontalAlignment.CENTER;

cellStyle.VerticalAlignment = VerticalAlignment.CENTER;

cellStyle.BorderBottom = CellBorderType.THIN;

cellStyle.BorderBottom = CellBorderType.THIN;

cellStyle.BottomBorderColor = NPOI.HSSF.Util.HSSFColor.BLACK.index;

cellStyle.BorderLeft = CellBorderType.THIN;

cellStyle.LeftBorderColor = NPOI.HSSF.Util.HSSFColor.BLACK.index;

cellStyle.BorderRight = CellBorderType.THIN;

cellStyle.RightBorderColor = NPOI.HSSF.Util.HSSFColor.BLACK.index;

cellStyle.BorderTop = CellBorderType.THIN;

cellStyle.TopBorderColor = NPOI.HSSF.Util.HSSFColor.BLACK.index;

int colIndex;

for (colIndex = 0; colIndex < dt.Columns.Count; colIndex++)

{

tagRow.CreateCell(colIndex).SetCellValue(dt.Columns[colIndex].ColumnName);

tagRow.GetCell(colIndex).CellStyle = cellStyle;

}

#endregion

#region 表数据

// 表数据

for (int k = 0; k < dt.Rows.Count; k++)

{

DataRow dr = dt.Rows[k];

NPOI.SS.UserModel.Row row = hssfSheet.CreateRow(k + 1);

for (int i = 0; i < dt.Columns.Count; i++)

{

row.CreateCell(i).SetCellValue(dr[i].ToString());

row.GetCell(i).CellStyle = cellStyle;

}

}

#endregion

FileStream file = new FileStream(HttpContext.Current.Request.PhysicalApplicationPath + "Temp/" + fileName + ".xls", FileMode.Create);

hssfworkbook.Write(file);

file.Close();

var basePath = VirtualPathUtility.AppendTrailingSlash(HttpContext.Current.Request.ApplicationPath);

return (basePath + "Temp/" + fileName + ".xls");

}

///

/// 去除HTML标记

///

///

/// 已经去除后的文字

public static string RemoveHtml(string htmlstring)

{

//删除脚本

htmlstring =

Regex.Replace(htmlstring, @"",

"", RegexOptions.IgnoreCase);

//删除HTML

htmlstring = Regex.Replace(htmlstring, @"]*)>", "", RegexOptions.IgnoreCase);

htmlstring = Regex.Replace(htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);

htmlstring = Regex.Replace(htmlstring, @"-->", "", RegexOptions.IgnoreCase);

htmlstring = Regex.Replace(htmlstring, @"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值