1. 程式人生 > >excel轉存pdf失敗,報錯

excel轉存pdf失敗,報錯

  問題:Excel轉存pdf失敗,報錯遠端過程呼叫失敗

轉存方法: 

 public static bool XLSConvertToPDF(string sourcePath, string targetPath)
{
bool result = false;
Excel.XlFixedFormatType targetType = Excel.XlFixedFormatType.xlTypePDF;
object missing = Type.Missing;
Excel.ApplicationClass application = null;
Excel.Workbook workBook = null;
try
{
application = new Excel.ApplicationClass();
object target = targetPath;
object type = targetType;
workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing, missing);
logger.Info("開始轉換成pdf");
workBook.ExportAsFixedFormat(targetType, target, Excel.XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
logger.Info("轉換成pdf完成");
result = true;
}
catch (Exception e)
{
result = false;
logger.Error(e.Message, e);
}
finally
{
if (workBook != null)
{
workBook.Close(true, missing, missing);
workBook = null;
}
if (application != null)
{
application.Quit();
application = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
return result;
}

解決思路:程式碼除錯沒有問題,釋出環境問題,呼叫com元件的許可權設定

解決方法:控制面板-元件服務—我的電腦—屬性—COM安全—編輯預設值  新增各種使用者許可權,主要是ASP使用者