【发布时间】:2012-10-15 05:30:27
【问题描述】:
我在尝试将 excel 2007(xlsx) 格式的工作簿导出为 pdf 时遇到上述错误。该工作簿仅包含一张工作表,我的 Windows 7 PC 上安装了 Ms Office 2007。 这是堆栈跟踪
at Microsoft.Office.Interop.Excel.WorkbookClass.ExportAsFixedFormat(XlFixedFormatType Type, Object Filename, Object Quality, Object IncludeDocProperties, Object IgnorePrintAreas, Object From, Object To, Object OpenAfterPublish, Object FixedFormatExtClassPtr)
at schoolwisereportprint.converttopdf() in
C:\Users\username\Desktop\Dpp2012New\schoolwisereportprint.aspx.vb:line 33
at schoolwisereportprint.Button1_Click(Object sender, EventArgs e) in C:\Users\username\Desktop\Dpp2012New\schoolwisereportprint.aspx.vb:line 158
这是我的 converttopdf() 函数:
Public Sub converttopdf()
Dim fileName As String = Server.MapPath("rahulserver/pdfreprt.xlsx")
Dim xlsApp = New Microsoft.Office.Interop.Excel.Application
xlsApp.ScreenUpdating = False
Dim xlsBook As Microsoft.Office.Interop.Excel.Workbook
Dim paramExportFormat As XlFixedFormatType = XlFixedFormatType.xlTypePDF
Dim paramExportQuality As XlFixedFormatQuality = XlFixedFormatQuality.xlQualityStandard
Dim paramOpenAfterPublish As Boolean = False
Dim paramIncludeDocProps As Boolean = True
Dim paramIgnorePrintAreas As Boolean = True
Dim paramFromPage As Object = Type.Missing
Dim paramToPage As Object = Type.Missing
xlsBook = xlsApp.Workbooks.Open(fileName, UpdateLinks:=False, ReadOnly:=False)
xlsBook.ExportAsFixedFormat(paramExportFormat, "rahulserver/pdfreprt" & ".pdf", paramExportQuality, paramIncludeDocProps, paramIgnorePrintAreas,paramFromPage, paramToPage, paramOpenAfterPublish)
xlsBook.Close(SaveChanges:=False)
xlsApp.Quit()
MsgBox("successfully converted to pdf!")
End Sub
即使将 paramFromPage 和 paramToPage 分别更改为 1 和 1 也无济于事。我已经花了一整天的时间来解决这个错误,但找不到方法。现在我期待 SO 让我摆脱困境......
【问题讨论】:
-
你有写服务器的权限吗?此外,我不确定你的路径是否正确——你需要一个“server.mappath”吗?作为事后的结果:Microsoft 不建议在服务器上使用互操作 - 也许您可以使用 EPPLUS 读取文件并使用 Itextsharp 将其写入 pdf。
-
还有问题吗?我遇到了同样的问题,我在本地尝试过,效果很好,我也尝试过使用 pdf995,它是第三方 pdf 转换器的 excel 也可以正常工作,它只是 exportasfixedformat 似乎没有工作,但是今天早些时候它确实工作,没有任何实质性的代码更改,所以我认为这是一个 excel 问题
-
@Alex 是的!我仍然有这个问题。我尽我所能,最后决定不在我的项目中使用该功能。
标签: .net vb.net ms-office export-to-pdf