使用Office.Interop.Excel将文件另存为PDF / A.

时间:2017-06-07 10:21:25

标签: vb.net vba office-interop excel-interop

如何将 Excel 电子表格导出为PDF / A (ISO 19005-1)?

编辑:我要求PDF / A,并且普通旧PDF 1.5,因为它默认导出。我甚至在我原来的问题中强调了 A

我已经可以使用UseISO19005_1函数将Word和PowerPoint文档导出为PDF / A,因为Word和PowerPoint函数都有一个可选的Dim ExportFormat As WdExportFormat = WdExportFormat.wdExportFormatPDF Dim OpenAfterExport As Boolean = False Dim OptimizeFor As WdExportOptimizeFor = WdExportOptimizeFor.wdExportOptimizeForPrint Dim Range As WdExportRange = WdExportRange.wdExportAllDocument Dim Item As WdExportItem = WdExportItem.wdExportDocumentWithMarkup Dim IncludeDocProps As Boolean = True Dim KeepIRM As Boolean = False Dim CreateBookmarks As WdExportCreateBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks Dim DocStructureTags As Boolean = True Dim BitmapMissingFonts As Boolean = True Dim UseISO19005_1 As Boolean = False If exportPDFA Then UseISO19005_1 = True Dim wordApp As New Word.Application() Dim doc As Word.Document = wordApp.Documents.Open(FileName) doc.ExportAsFixedFormat(pathToDestFile, ExportFormat, OpenAfterExport, OptimizeFor, Range, 0, 0, Item, IncludeDocProps, KeepIRM, CreateBookmarks, DocStructureTags, BitmapMissingFonts, UseISO19005_1) End If 参数,但Excel版本非常不同,并且缺少很多参数。

我似乎找不到使用COM Interop导出PDF / A 的方法。

这是我用来从docx导出的代码:

ExportAsFixedFormat()

但对于xlsx,Sub ExportAsFixedFormat(Type As XlFixedFormatType, Optional Filename As Object = Nothing, Optional Quality As Object = Nothing, Optional IncludeDocProperties As Object = Nothing, Optional IgnorePrintAreas As Object = Nothing, Optional From As Object = Nothing, Optional [To] As Object = Nothing, Optional OpenAfterPublish As Object = Nothing, Optional FixedFormatExtClassPtr As Object = Nothing) 函数接受非常不同的参数(这是直接从Microsoft.Office.Interop.Excel类中获取的):

{{1}}

2 个答案:

答案 0 :(得分:3)

Excel允许用户选择是否在保存对话框的选项中将文件另存为PDF / A.此设置作为LastISO19005-1(REG_DWORD)存储在注册表中HKEY_CURRENT_USER \ Software \ Microsoft \ Office \ 12.0 \ Common \ FixedFormat下。 (将12.0替换为正确的版本。)ExportAsFixedFormat函数也遵循此设置。在调用ExportAsFixedFormat以使Excel将文件导出为PDF / A之前,可以将此值设置为1.

我知道这个解决方案并不漂亮,因为它使用全局状态来解决本地问题。在您完成后,请考虑恢复之前的值。

答案 1 :(得分:0)

使用以下方法将Excel转换为pdf并将其标记为答案(如果有效)

Public Function ExportWorkbookToPdf(ByVal workbookPath As String, ByVal outputPath As String) As Boolean

    ' If either required string is null or empty, stop and bail out
    If String.IsNullOrEmpty(workbookPath) OrElse String.IsNullOrEmpty(outputPath) Then
        Return False
    End If

    ' Create COM Objects
    Dim excelApplication As Microsoft.Office.Interop.Excel.Application
    Dim excelWorkbook As Microsoft.Office.Interop.Excel.Workbook

    ' 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.Open(workbookPath)

    ' If the workbook failed to open, stop, clean up, and bail out
    If excelWorkbook Is Nothing Then
        excelApplication.Quit()

        excelApplication = Nothing
        excelWorkbook = Nothing

        Return False
    End If

    Dim exportSuccessful = True
    Try
        ' Call Excel's native export function (valid in Office 2007 and Office 2010, AFAIK)
        excelWorkbook.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, outputPath)
    Catch ex As System.Exception
        ' Mark the export as failed for the return value...

        ' Do something with any exceptions here, if you wish...
        ' MessageBox.Show...        
        exportSuccessful = False
    Finally
        ' Close the workbook, quit the Excel, and clean up regardless of the results...
        excelWorkbook.Close()
        excelApplication.Quit()

        excelApplication = Nothing
        excelWorkbook = Nothing
    End Try

    ' 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) Then
        System.Diagnostics.Process.Start(outputPath)
    End If

    Return exportSuccessful
End Function