从字符串中删除找到特定 HTML 标记的行

发布于 2024-10-19 16:59:58 字数 3281 浏览 0 评论 0 原文

我有一个通过 MVC 操作导出到 Excel 文件的 HTML 页面。该操作实际上会渲染此部分视图,然后将具有正确格式的渲染视图导出到 Excel 文件。但是,该视图的呈现方式与我执行导出之前所看到的完全一样,并且该视图包含一个“导出到 Excel”按钮,因此当我导出此按钮时,该按钮图像在视图的左上角显示为红色 X。 Excel 文件。

我可以拦截包含此 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>

顶部的 标记是我想要的标记删除只是为了导出。您看到的所有内容都包含在 C# 字符串中。我该如何从字符串中删除该行,这样它就不会尝试在 Excel 中渲染图像?

编辑:可能也有道理,我们在导出中也不需要任何

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

幸福不弃 2024-10-26 16:59:58

删除所有 img 标签:

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

包括参考:

using System.Text.RegularExpressions;

Remove all img tags:

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

Include reference:

using System.Text.RegularExpressions;

无名指的心愿 2024-10-26 16:59:58

如果它在 C# 字符串中,则只需:

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

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" />","");
西瓜 2024-10-26 16:59:58

最安全的方法是使用 HTML Agility Pack 读取 HTML,然后编写代码从 HTML 中删除图像节点。

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

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

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.

逆蝶 2024-10-26 16:59:58

我只是用这个

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

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();
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文