我正在处理的环境是如下:
- Windows 10
- Visual Studio 2016
- Office 2016
我已成功设法打开Excel表,并保存到其他位置。但是,当尝试打开excel并保存为pdf时,我收到以下错误消息:
服务器抛出异常。 (HRESULT的异常:0x80010105(RPC_E_SERVERFAULT))
查看错误的详细信息,我可以看到以下内容:
System.Runtime.InteropServices.COMException {服务器抛出异常(来自HRESULT的异常:0x80010105(RPC_E_SERVERFAULT))}
错误代码:-2147417851
我正在使用的代码如下:
Dim xlApp As New Excel。应用程序 Dim xlWorkBook作为Excel.Workbook Dim xlWorkSheet作为Excel.Worksheet xlApp.Visible = False xlApp.AlertBeforeOverwriting = False Dim sheetname As String =d:\test\test.xlsx xlWorkBook = xlApp.Workbooks.Open(sheetname) xlWorkBook.Activate() xlWorkSheet = xlWorkBook.Sheets(Sheet1) xlWorkSheet.Activate() xlApp.DisplayAlerts = False xlWorkSheet.ExportAsFixedFormat(Type:= Excel.XlFixedFormatType.xlTypePDF,Filename:= $ b $ bd:\test\test.pdf,质量:= Excel.XlFixedFormatQuality.xlQualityStandard _ ,IncludeDocProperties:= True,IgnorePrintAreas:= True,OpenAfterPublish:= False) xlWorkBook .Close(SaveChanges:= False) xlApp.Quit() System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp):xlApp = Nothing System.Runtime.InteropServices.Marshal.ReleaseComObject (xlWorkBook):xlWorkBook = Nothing System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheet):xlWorkSheet = Nothing帮助!
由于Excel表格具有公式,将内容复制并粘贴到新工作表中(粘贴特殊值和数字格式)和运行上面的代码
解决方案对!感谢所有试图帮助的人。通过阅读您的回复,我一直在努力,直到我想出了这个问题。
看来,ExportAsFixedFormat不喜欢带有公式的Excel表格。为了解决这个问题,我刚刚创建了一个新的空白工作表,并将内容从我的主页复制并粘贴(仅限于值)。这似乎工作完美。然后我在我的代码中自动化如下:
Dim xlworksheet_static As Excel.Worksheet = xlWorkBook.Worksheets(2) xlWorkSheet.Range(A1,H35)。Copy() xlworksheet_static.Activate() xlworksheet_static.Range(A1,H35)。 xlworksheet_static.PasteSpecial(Excel.XlPasteType.xlPasteValuesAndNumberFormats) xlworksheet_static.ExportAsFixedFormat(Type:= Excel.XlFixedFormatType.xlTypePDF,Filename:= $ b $ bd:\test\test.pdf ,质量:= Excel.XlFixedFormatQuality.xlQualityStandard _ ,IncludeDocProperties:= True,IgnorePrintAreas:= True,OpenAfterPublish:= False)可能不是最直接的方式,但我无法让它工作,否则!
I am trying to write a very simple VB app which would open an excel file and save it as excel.
The environment I am working on is as follows:
- Windows 10
- Visual Studio 2016
- Office 2016
I have successfully managed to open the excel sheet and save to another location. However when trying to open the excel and save as pdf, I get the following error message
The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT))
Looking into the details of the error, I can see the following:
System.Runtime.InteropServices.COMException {"The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT))"}
error code: -2147417851
The code I am using to do this is as follows:
Dim xlApp As New Excel.Application Dim xlWorkBook As Excel.Workbook Dim xlWorkSheet As Excel.Worksheet xlApp.Visible = False xlApp.AlertBeforeOverwriting = False Dim sheetname As String = "d:\test\test.xlsx" xlWorkBook = xlApp.Workbooks.Open(sheetname) xlWorkBook.Activate() xlWorkSheet = xlWorkBook.Sheets("Sheet1") xlWorkSheet.Activate() xlApp.DisplayAlerts = False xlWorkSheet.ExportAsFixedFormat(Type:=Excel.XlFixedFormatType.xlTypePDF, Filename:= "d:\test\test.pdf", Quality:=Excel.XlFixedFormatQuality.xlQualityStandard _ , IncludeDocProperties:=True, IgnorePrintAreas:=True, OpenAfterPublish:= False) xlWorkBook.Close(SaveChanges:=False) xlApp.Quit() System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp) : xlApp = Nothing System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook) : xlWorkBook = Nothing System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheet) : xlWorkSheet = NothingHelp!
issue seem to arise due to the Excel sheet having formulas, copying and pasting the content into a new sheet (paste special values and number formatting only) and running the code above works
解决方案Right! Thank you to all that tried to help. By reading your responses I kept trying till I figured out the issue.
It appears that ExportAsFixedFormat doesn't like Excel sheets with formulas in. To work around this issue, I just created a new blank worksheet and copied and pasted (values only) the content from my main sheet into it. This seems to work perfectly. I then automated this in my code as follows:
Dim xlworksheet_static As Excel.Worksheet = xlWorkBook.Worksheets(2) xlWorkSheet.Range("A1", "H35").Copy() xlworksheet_static.Activate() xlworksheet_static.Range("A1", "H35").Select() xlworksheet_static.PasteSpecial(Excel.XlPasteType.xlPasteValuesAndNumberFormats) xlworksheet_static.ExportAsFixedFormat(Type:=Excel.XlFixedFormatType.xlTypePDF, Filename:= "d:\test\test.pdf", Quality:=Excel.XlFixedFormatQuality.xlQualityStandard _ , IncludeDocProperties:=True, IgnorePrintAreas:=True, OpenAfterPublish:= False)Probably not the most straight forward way but I couldn't get it to work otherwise!
更多推荐
VB.net Excel ExportAsFixedFormat失败从HRESULT的异常:0x80010105(RPC
发布评论