I wanted to know if there is a way I can get the content of a Web Browser control and save it in a format that would open in Excel? Currently I have an application that gets data from the user and converts it to a table and displays it in html in a WPF Web Browser control. I can export this controls content successfully as a PDF document as follows:
string fileName = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".html";
File.WriteAllText(fileName, generateHtml());
//Creates a new instance of the Microsoft office interop for using MS Word
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
app.Visible = false;
Microsoft.Office.Interop.Word.Document doc = app.Documents.Open(fileName);
doc.SaveAs("C:\\Temp\\testExport.pdf", 17);
app.Quit();
This works fine for exporting a PDF file, the issue I am having is that I can’t for the life of me get this to work with an excel file. So far I am trying to do it like so:
string filePath = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".html";
File.WriteAllText(filePath, generateHtml());
//Creates a new instance of Microsoft office interop for using MS Excel
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
//Create a new instance of a workbook
Microsoft.Office.Interop.Excel.Workbook excelWorkBook = excelApp.Workbooks.Add();
//Don't Show MS Excel
excelApp.Visible = false;
//Open a file at location
Microsoft.Office.Interop.Excel.Workbook excelWorkBook2 = excelApp.Workbooks.Open(filePath);
//Convert that file to an XLSX document
excelWorkBook2.SaveAs("C:\\Temp\\testExport.xlsx", Microsoft.Office.Interop.Excel.XlFileFormat.xlXMLSpreadsheet, Missing.Value, Missing.Value, false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Microsoft.Office.Interop.Excel.XlSaveConflictResolution.xlUserResolution, true, Missing.Value, Missing.Value, Missing.Value);
//Close excel
excelApp.Quit();
However when I try and open the file that this generates I get the following error:

Have you tried changing the filename to .xls? I believe that Microsoft.Office.Interop.Excel.XlFileFormat.xlXMLSpreadsheet exports to xls, not xlsx
All the types can be found here