2011-03-02 5 views
0

このHTMLページは、MVCアクションによってExcelファイルにエクスポートされます。アクションは実際に行ってこの部分的なビューをレンダリングし、レンダリングしたビューを正しい書式でExcelファイルにエクスポートします。ただし、エクスポートを実行する前にビューがどのように表示され、そのビューには「Excelにエクスポート」ボタンが含まれているため、エクスポートするとボタン画像はExcelファイル。特定のHTMLタグが見つかった文字列から行を削除する

私はExcelExportアクションでレンダリングするには、このHTMLを含む文字列を傍受することができ、そしてそれは、一例のために次のようになります。

<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でイメージをレンダリングしないでください。

EDIT:<script>のいずれもエクスポートする必要はありませんが、それでもExcelには表示されないので、今はそれほど大したことではないと思います。

答えて

7

すべてのimgタグを削除します。

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

参照を含める:

using System.Text.RegularExpressions;

+0

このように私のものより良い。ありがとう – slandau

0

それだけで、その後C#の文字列になら:

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

を使用してまあ、私は(私が思う?)を削除したい、すべての画像でよ文字列したがって、というタグはすべて、Excelが1つをレンダリングできない場合は、間違いなくそれをレンダリングできません。 – slandau

+0

また、そのパラメータに引用符の書式設定に問題があると思います。それは編集していない。 – slandau

0

これを行うための最も安全な方法は、HTMLを読み込み、その後、HTMLから画像ノードを削除するコードを記述するHTML Agility Packを使用するようになります。

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

あなたはscriptタグや他のimgタグを削除するようなコードを使用することができます。

+0

これを変更する必要がありました - >プライベート文字列RemoveImages(文字列html) { HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(html); HtmlNode image = doc.GetElementbyId( "ExportToExcel"); image.Remove(); html = doc.ToString(); return html; } - >コンパイルするには、しかし、それはまったく正しいものを返すわけではありません。 – slandau

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(); 
     } 
関連する問題