Search code examples
c#.netasp.net-mvc-2stringexport-to-excel

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


Solution

  • Remove all img tags:

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

    Include reference:

    using System.Text.RegularExpressions;