Export dynamically generated webbrowser control content to excel
I have a windows application in which an html report is displayed using the webbrowser control. The content of webbrrowser control is generated dynam开发者_JAVA技巧ically and provided as below
webbrowser1.DocumentText=htmlString;
Now I want to export the webbrowser control content to excel on clicking the "Export to Excel button".
First thing you can do is to create an excel template. Than you can open this template edit it and save it to a different location . This sample snippet can help you.
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();
}
}
You can use html agility pack to parse your html.
精彩评论