将Excel转换为PDF时出错 [英] Getting error when convert excel to PDF
本文介绍了将Excel转换为PDF时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
将Excel转换为PDF时面临错误
帮助我
来自HRESULT的异常:0x800A03EC
我尝试了什么:
Facing Error When Convert Excel to PDF
Help me
Exception from HRESULT: 0x800A03EC
What I have tried:
public static bool ExportWorkbookToPdf(string workbookPath, string outputPath)
{
// If either required string is null or empty, stop and bail out
if (string.IsNullOrEmpty(workbookPath) || string.IsNullOrEmpty(outputPath))
{
return false;
}
// Create COM Objects
Microsoft.Office.Interop.Excel.Application excelApplication;
Microsoft.Office.Interop.Excel.Workbook excelWorkbook;
// Create new instance of Excel
excelApplication = new Microsoft.Office.Interop.Excel.Application();
// Make the process invisible to the user
excelApplication.ScreenUpdating = false;
// Make the process silent
excelApplication.DisplayAlerts = false;
// Open the workbook that you wish to export to PDF
excelWorkbook = excelApplication.Workbooks.Add(workbookPath);
// If the workbook failed to open, stop, clean up, and bail out
if (excelWorkbook == null)
{
excelApplication.Quit();
excelApplication = null;
excelWorkbook = null;
return false;
}
var exportSuccessful = true;
try
{
excelWorkbook.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, outputPath);
}
catch (System.Exception ex)
{
// Mark the export as failed for the return value...
exportSuccessful = false;
// Do something with any exceptions here, if you wish...
// MessageBox.Show...
}
finally
{
// Close the workbook, quit the Excel, and clean up regardless of the results...
excelWorkbook.Close();
excelApplication.Quit();
excelApplication = null;
excelWorkbook = null;
}
// You can use the following method to automatically open the PDF after export if you wish
// Make sure that the file actually exists first...
if (System.IO.File.Exists(outputPath))
{
// System.Diagnostics.Process.Start(outputPath);
}
return exportSuccessful;
}
错误是
---- -----
来自HRESULT的异常:0x800A03EC
Error is
---------
Exception from HRESULT: 0x800A03EC
推荐答案
您没有打开现有的工作簿。相反,你正在创建一个新的(空)。因此,您应该将代码更改为:
You are not opening an existing workbook. Instead you are creating a new (empty) one. So you should change your code to be:
//excelWorkbook = excelApplication.Workbooks.Add(workbookPath);
excelWorkbook = excelApplication.Workbooks.Open(workbookPath);
这篇关于将Excel转换为PDF时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文