2011-03-02 54 views
0

所以我有这个HTML页面通过MVC操作导出到Excel文件。该操作实际上会执行并呈现此部分视图,然后将具有正确格式的呈现视图导出到Excel文件。然而,看法刚好呈现它是如何我做出口前看到,而且视图包含一个“导出到Excel”按钮,所以当我导出此,按钮图像显示为的左上角红色的X Excel文件。从字符串中删除行,其中发现某个HTML标记

我可以拦截包含此HTML代码在为ExcelExport动作来呈现字符串,它看起来像这样的一个例子:

<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> 

在顶部<img id="ExportToExcel"标签是一个我想删除只为出口。你看到的所有内容都包含在一个C#字符串中。我将如何去除并从字符串中删除该行,以便它不尝试在Excel中呈现图像?

编辑:也许也有道理,我们不会需要在出口<script>任何任何,但既然这将不会出现在Excel中,无论如何,我不认为这是一个巨大的交易。

回答

7

删除所有的img标签:

string html2 = Regex.Replace(html, @"(<img\/?[^>]+>)", @"", 
    RegexOptions.IgnoreCase); 

序号:

using System.Text.RegularExpressions;

+0

像这样比我好。谢谢 – slandau 2011-03-02 18:16:09

0

如果它是在C#中的字符串,然后只是:

myHTMLString.Replace(@"<img id="ExportToExcel" style=" cursor: pointer;" src="/Extranet/img/btn_user_export_excel_off.gif" />",""); 
+0

嗯,我想删除(我想?),在所有图片字符串。所以这是标签,因为如果Excel不能呈现一个,它肯定会不能够提供任何所有标签。 – slandau 2011-03-02 16:40:02

+0

此外,我认为该参数中的报价格式有问题。这不是编译。 – slandau 2011-03-02 16:44:15

0

做到这一点,最安全的方式将是使用HTML Agility Pack在HTML阅读,然后编写代码,将删除HTML图像节点。

HtmlDocument doc = new HtmlDocument(); 
doc.LoadHtml(htmlString); 
HtmlNode image =doc.GetElementById("ExportToExcel"]); 
image.Remove(); 
htmlString = doc.WriteTo(); 

您可以使用类似的代码删除script标签和其他img标签。

+0

只好它更改为这个 - >私人字符串RemoveImages(串HTML) { 的HTMLDocument DOC =新的HTMLDocument(); doc.LoadHtml(html); HtmlNode image = doc.GetElementbyId(“ExportToExcel”); image.Remove(); html = doc.ToString(); return html; } - >为它编译,但它根本没有返回正确的东西。 – slandau 2011-03-02 16:59:01

0

我只是用这个

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(); 
     }