使用 C# 将 HTML 字符串导出到 ASP.Net 中的 Excel 文件
Export HTML string to Excel file in ASP.Net using C#
我在数据库 MySql version 8.0.17
中有这个 table,其中包含字段 contents
上的 HTML 代码
<p><h3><span style=color:#0000ff;><strong>test</strong></span></h3></p>
<h4><strong>- test1</strong></h4>
<h4><strong>- test2</strong></h4>
我不是数据库管理员,所以我只能从这个 table 中读取,我无法修改..
在 xls 文件上以 excel 格式导出此 table 时,找到所有 HTML 标签
如何解决这个问题?
在此先感谢您的帮助。
下面是我的代码
private void MTxlssp()
{
MySqlCommand cmd =
new MySqlCommand("SP");
DataTable dt = GetData(cmd);
GridView GridView1 = new GridView
{
AllowPaging = false,
DataSource = dt
};
GridView1.DataBind();
Thread.Sleep(3000);
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", attachment;filename=\"test.xls\"");
Response.ContentEncoding = Encoding.UTF8;
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
HttpCookie cookie2 = new HttpCookie("ExcelDownloadFlag")
{
Value = "Flag",
Expires = DateTime.Now.AddDays(1)
};
Response.AppendCookie(cookie2);
using (StringWriter sw = new StringWriter())
{
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.HeaderRow.BackColor = Color.White;
foreach (TableCell cell in GridView1.HeaderRow.Cells)
{
cell.BackColor = GridView1.HeaderStyle.BackColor;
}
foreach (GridViewRow row in GridView1.Rows)
{
row.BackColor = Color.White;
foreach (TableCell cell in row.Cells)
{
if (row.RowIndex % 2 == 0)
{
cell.BackColor = GridView1.AlternatingRowStyle.BackColor;
}
else
{
cell.BackColor = GridView1.RowStyle.BackColor;
}
cell.CssClass = "textmode";
}
}
GridView1.RenderControl(hw);
string style = @"<style> .textmode { } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
}
如果它只是您需要的显示文本,那么 Nuglify 库也许可以帮助您。它支持从 HTML:
提取文本
var html = @"<p><h3><span style=color:#0000ff;><strong>test</strong></span></h3></p>
<h4><strong>- test1</strong></h4>
<h4><strong>- test2</strong></h4>";
var result = Uglify.HtmlToText(html);
Console.WriteLine(result.Code);
您可以从此处下载:https://github.com/trullock/NUglify 或从 Nuget 获取。
我刚刚 运行 它在你的 html 样本上,它产生:
test - test1 - test2
根据你对@Ivan Khorin 的评论,我假设这就是你想要的
我在数据库 MySql version 8.0.17
中有这个 table,其中包含字段 contents
<p><h3><span style=color:#0000ff;><strong>test</strong></span></h3></p>
<h4><strong>- test1</strong></h4>
<h4><strong>- test2</strong></h4>
我不是数据库管理员,所以我只能从这个 table 中读取,我无法修改..
在 xls 文件上以 excel 格式导出此 table 时,找到所有 HTML 标签
如何解决这个问题?
在此先感谢您的帮助。
下面是我的代码
private void MTxlssp()
{
MySqlCommand cmd =
new MySqlCommand("SP");
DataTable dt = GetData(cmd);
GridView GridView1 = new GridView
{
AllowPaging = false,
DataSource = dt
};
GridView1.DataBind();
Thread.Sleep(3000);
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", attachment;filename=\"test.xls\"");
Response.ContentEncoding = Encoding.UTF8;
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
HttpCookie cookie2 = new HttpCookie("ExcelDownloadFlag")
{
Value = "Flag",
Expires = DateTime.Now.AddDays(1)
};
Response.AppendCookie(cookie2);
using (StringWriter sw = new StringWriter())
{
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.HeaderRow.BackColor = Color.White;
foreach (TableCell cell in GridView1.HeaderRow.Cells)
{
cell.BackColor = GridView1.HeaderStyle.BackColor;
}
foreach (GridViewRow row in GridView1.Rows)
{
row.BackColor = Color.White;
foreach (TableCell cell in row.Cells)
{
if (row.RowIndex % 2 == 0)
{
cell.BackColor = GridView1.AlternatingRowStyle.BackColor;
}
else
{
cell.BackColor = GridView1.RowStyle.BackColor;
}
cell.CssClass = "textmode";
}
}
GridView1.RenderControl(hw);
string style = @"<style> .textmode { } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
}
如果它只是您需要的显示文本,那么 Nuglify 库也许可以帮助您。它支持从 HTML:
提取文本var html = @"<p><h3><span style=color:#0000ff;><strong>test</strong></span></h3></p>
<h4><strong>- test1</strong></h4>
<h4><strong>- test2</strong></h4>";
var result = Uglify.HtmlToText(html);
Console.WriteLine(result.Code);
您可以从此处下载:https://github.com/trullock/NUglify 或从 Nuget 获取。
我刚刚 运行 它在你的 html 样本上,它产生:
test - test1 - test2
根据你对@Ivan Khorin 的评论,我假设这就是你想要的