【发布时间】:2014-01-05 20:10:52
【问题描述】:
我遇到了需要更改 excel 页面设置(方向缩放或任何属性,但默认打印机不可用,我没有访问打印机的权限或发现此代码引发异常的问题) .
xlApp = new Excel.Application();
book = xlApp.Workbooks.Open(fileFullPath);
xlApp.Visible = false;
xlApp.DisplayAlerts = false;
var with1 = book.Worksheets[sheetName].PageSetup;
with1.zoom = false;
with1.FitToPagesWide = 1;
with1.FitToPagesTall = 1;
with1.Orientation = XlPageOrientation.xlLandscape;
例外是:
Exception: System.Runtime.InteropServices.COMException
Message: Unable to set the Zoom property of the PageSetup class
Target Site: Void CheckThrowException(Int32, System.Dynamic.ExcepInfo ByRef, UInt32, System.String)
Source: Microsoft Excel
Stack Trace: at System.Dynamic.ComRuntimeHelpers.CheckThrowException(Int32 hresult, ExcepInfo& excepInfo, UInt32 argErr, String message)
at CallSite.Target(Closure , CallSite , ComObject , Boolean )
at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
at CallSite.Target(Closure , CallSite , Object , Boolean )
at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
at ExcelProcessing.ThumbGeneration.ExcelSheetSnapShoot(String fileFullPath, String thumbDir, String sheetName, String thumbResolution, String cropArea, String PDFFilesDir)
注意: 当没有找到默认打印机时发生异常,更改页面布局属性时抛出此异常,所以我需要任何其他东西将 excel 文件导出为 lanscape PDF
【问题讨论】:
-
哪里有什么例外? (
xlApp.Visible = false没用,因为创建后是不可见的) -
我添加了当前用户无法访问默认打印机(找不到打印机或没有权限)时发生此错误的异常