开发者

Remove line from string where certain HTML tag is found

So I have this HTML page that is exported to an Excel file through an MVC action. The action actually goes and renders this partial view, and then exports that rendered view with correct formatting to an Excel file. However, the view is rendered exactly how it is seen before I do the export, and that view contains an "Export to Excel" button, so when I export this, the button image appears as a red X in the top left corner of the Excel file.

I can intercept the string containing this HTML to render in the ExcelExport action, and it looks like this for one example:

<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">
          开发者_如何学JAVA              <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>

That <img id="ExportToExcel" tag at the top is the one I want to remove just for the export. All of what you see is contained within a C# string. How would I go and remove that line from the string so it doesn't try and render the image in Excel?

EDIT: Would probably make sense also that we wouldn't need any of the <script> in the export either, but since that won't show up in Excel anyway I don't think that's a huge deal for now.


Remove all img tags:

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

Include reference:

using System.Text.RegularExpressions;


If it's in a C# string then just:

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


The safest way to do this will be to use the HTML Agility Pack to read in the HTML and then write code that removes the image node from the HTML.

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

You can use similar code to remove the script tag and other img tags.


I'm just using this

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜