【问题标题】:Export dynamically generated webbrowser control content to excel将动态生成的 webbrowser 控件内容导出到 excel
【发布时间】:2011-08-25 16:25:48
【问题描述】:

我有一个 Windows 应用程序,其中使用 webbrowser 控件显示 html 报告。浏览器控件的内容是动态生成的,提供如下

 webbrowser1.DocumentText=htmlString;

现在我想在单击“导出到 Excel 按钮”时将 webbrowser 控件内容导出到 excel。

【问题讨论】:

    标签: c# html webbrowser-control export-to-excel


    【解决方案1】:

    您可以做的第一件事是创建一个 Excel 模板。您可以打开此模板进行编辑并将其保存到其他位置。这个示例 sn-p 可以帮助您。

            using System;
            using Excel = Microsoft.Office.Interop.Excel;
    
            class Program
            {
                static void Main(string[] args)
                {
                    var excelApplication = new Excel.Application { Visible = false, DisplayAlerts = false };
                    Excel.Workbook excelWorkBook = excelApplication.Workbooks.Open(Filename: @"C:\temp\YourTemplate.xlsx");
                    Excel.Worksheet targetedExcelSheet = (Excel.Worksheet)excelApplication.ActiveWorkbook.Sheets[1];
                    Excel.Range ATrialRange = targetedExcelSheet.Range["F4", Type.Missing];
                    ATrialRange.Value2 = "The values that you have parsed from your html string";
                    excelWorkBook.SaveAs(Filename: @"C:\temp\YourNewlyGenerated.xlsx");
                    excelWorkBook.Close();
                    excelApplication.Quit();    
                }
            }
    

    您可以使用 html 敏捷包来解析您的 html。

    【讨论】:

    • 我没有得到这行代码 sn -p ATrialRange.Value2 = "The values that you have parsed from your html string";
    • 我的建议是手动解析您的 html 字符串,从您的 html 中提取值。将其写入您的 Excel 工作表。我不知道有任何其他方法可以从您的网络浏览器控件中导出 Excel 工作表。
    • 使用 iterop 的东西要非常小心 - 确保你自己清理,处理异常,关闭文件等等,因为它们都不是垃圾收集的。
    • 执行 excelApplication.Quit();行不收拾烂摊子?我应该手动为它们分配 null 吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-11
    • 2012-04-09
    • 1970-01-01
    相关资源
    最近更新 更多