2011-03-02 56 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(); 
     }