你好,这是我的问题。我目前有一个独立的WPF应用程序,它具有Web浏览器控件。我有一些C#中的代码生成一个HTML页面,我将其用作Web浏览器的内容。我的问题是我正在尝试将此HTML页面导出为excel并将图像包含在其中。这就是我当前将HTML导出到EXCEL文件的方式:
string filePath = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".html";
//Creates an HTML file from the web browser and save it to a temp location
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
SaveFileDialog newSFD = new SaveFileDialog();
newSFD.FileName = "Report in Excel Format";
newSFD.DefaultExt = ".xls";
newSFD.Filter = "XLS Documents (.xls)|*.xls";
Nullable<bool> result = newSFD.ShowDialog();
if (result == true)
{
string fileName = newSFD.FileName;
excelWorkBook2.SaveAs(fileName, 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();
}
一切都出口好,所有信息都在那里,但由于某种原因没有图像。图像应该在那里的表格,它有图像应该去的地方持有人,这就是为什么我这么困惑。如果需要更多解释,请告诉我,我会提供,否则感谢您对此问题的帮助/关注。
答案 0 :(得分:0)
经过大量的挖掘/反复试验和研究后,我找到了解决办法,并且一如既往地在你面前。问题就在于我将XML文档保存为excel文件,如我在以这种方式处理它的问题中所示:
excelWorkBook2.SaveAs(fileName, 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);
实际上我应该像这样保存它:
excelWorkBook2.SaveAs(fileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, 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);
两者之间的区别在于,我不是将XML文档保存为xlXMLSpreadsheet,而是将文件保存为xlWorkBookNormal。一旦我做了这个改变,它允许我将我的文件保存为excel文档并包含图像。简单解决我认为更有趣的问题。