C# 从找到特定HTML标记的字符串中删除行
所以我有这个HTML页面,它通过MVC操作导出到Excel文件。实际上,该操作将执行并渲染此局部视图,然后将具有正确格式的渲染视图导出到Excel文件。但是,视图的呈现方式与我导出之前的显示方式完全相同,并且该视图包含一个“导出到Excel”按钮,因此当我导出该按钮时,按钮图像在Excel文件的左上角显示为红色X 我可以截取包含此HTML的字符串以在ExcelExport操作中呈现,例如:C# 从找到特定HTML标记的字符串中删除行,c#,.net,asp.net-mvc-2,string,export-to-excel,C#,.net,Asp.net Mvc 2,String,Export To Excel,所以我有这个HTML页面,它通过MVC操作导出到Excel文件。实际上,该操作将执行并渲染此局部视图,然后将具有正确格式的渲染视图导出到Excel文件。但是,视图的呈现方式与我导出之前的显示方式完全相同,并且该视图包含一个“导出到Excel”按钮,因此当我导出该按钮时,按钮图像在Excel文件的左上角显示为红色X 我可以截取包含此HTML的字符串以在ExcelExport操作中呈现,例如: <div id="summaryInformation" > <img id="Expo
<div id="summaryInformation" >
<img id="ExportToExcel" style=" cursor: pointer;" src="/Extranet/img/btn_user_export_excel_off.gif" />
<table class="resultsGrid" cellpadding="2" cellspacing="0">
<tr>
<td id="NicknameLabel" class="resultsCell">Nick Name</td>
<td id="NicknameValue" colspan="3">
Swap
</td>
</tr>
<tr>
<td id="EffectiveDateLabel" class="resultsCell">
<label for="EffectiveDate">Effective Date</label>
</td>
<td id="EffectiveDateValue" class="alignRight">
02-Mar-2011
</td>
<td id ="NotionalLabel" class="resultsCell">
<label for="Notional">Notional</label>
</td>
<td id="NotionalValue" class="alignRight">
<span>
USD
</span>
10,000,000.00
</td>
</tr>
<tr>
<td id="MaturityDateLabel" class="resultsCell">
<label for="MaturityDate">Maturity Date</label>
</td>
<td id="MaturityDateValue" class="alignRight">
02-Mar-2016
-
Modified Following
</td>
<td id="TimeStampLabel" class="resultsCell">
Rate Time Stamp
</td>
<td id="Timestamp" class="alignRight">
28-Feb-2011 16:00
</td>
</tr>
<tr >
<td id="HolidatCityLabel" class="resultsCell"> Holiday City</td>
<td id="ddlHolidayCity" colspan="3">
New York,
London
</td>
</tr>
</table>
</div>
<script>
$("#ExportToExcel").click(function () {
// ajax call to do the export
var actionUrl = "/Extranet/mvc/Indications.cfc/ExportToExcel";
var viewName = "/Extranet/Views/Indications/ResultsViews/SummaryInformation.aspx";
var fileName = 'SummaryInfo.xls';
GridExport(actionUrl, viewName, fileName);
});
</script>
昵称
交换
生效日期
2011年3月2日
理论上的
美元
10,000,000.00
到期日
2016年3月2日
-
修改后的跟踪
速率时间戳
2011年2月28日16:00
假日城市
纽约,
伦敦
$(“#ExportToExcel”)。单击(函数(){
//进行导出的ajax调用
var actionUrl=“/Extranet/mvc/Indications.cfc/ExportToExcel”;
var viewName=“/Extranet/Views/Indications/ResultsViews/SummaryInformation.aspx”;
var fileName='SummaryInfo.xls';
GridExport(actionUrl、viewName、文件名);
});
那如果它是C#字符串,那么只需:
myHTMLString.Replace(@"<img id="ExportToExcel" style=" cursor: pointer;" src="/Extranet/img/btn_user_export_excel_off.gif" />","");
myHTMLString.Replace(@“”);
最安全的方法是使用读取HTML,然后编写从HTML中删除图像节点的代码
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(htmlString);
HtmlNode image =doc.GetElementById("ExportToExcel"]);
image.Remove();
htmlString = doc.WriteTo();
您可以使用类似的代码删除脚本
标记和其他img
标记。删除所有img标记:
string html2 = Regex.Replace( html, @"(<img\/?[^>]+>)", @"",
RegexOptions.IgnoreCase );
我只是在用这个
private string RemoveImages(string html)
{
StringBuilder retval = new StringBuilder();
using (StringReader reader = new StringReader(html))
{
string line = string.Empty;
do
{
line = reader.ReadLine();
if (line != null)
{
if (!line.StartsWith("<img"))
{
retval.Append(line);
}
}
} while (line != null);
}
return retval.ToString();
}
私有字符串移除图像(字符串html)
{
StringBuilder retval=新建StringBuilder();
使用(StringReader=new StringReader(html))
{
string line=string.Empty;
做
{
line=reader.ReadLine();
如果(行!=null)
{
如果(!line.StartsWith(“嗯,我想删除字符串中的所有图像。所以所有标记都是标记,因为如果Excel不能呈现一个标记,它肯定不能呈现任何标记。另外,我认为该参数中的引号格式有问题。它没有编译。必须将其更改为-->私有字符串RemoveImages。”(字符串html){HtmlDocument doc=new HtmlDocument();doc.LoadHtml(html);HtmlNode image=doc.GetElementbyId(“ExportToExcel”);image.Remove();html=doc.ToString();return html;}-->让它编译,但它根本不会返回正确的内容。像这样比我的好。谢谢
private string RemoveImages(string html)
{
StringBuilder retval = new StringBuilder();
using (StringReader reader = new StringReader(html))
{
string line = string.Empty;
do
{
line = reader.ReadLine();
if (line != null)
{
if (!line.StartsWith("<img"))
{
retval.Append(line);
}
}
} while (line != null);
}
return retval.ToString();
}