使用 COM 自动化时 Excel 迷你图未导出为 PDF

发布于 2024-10-02 13:23:37 字数 2684 浏览 0 评论 0原文

我在以编程方式将包含迷你图的 Excel 工作表导出为 PDF 格式时遇到问题。

当我使用 Excel 2010 的本机 PDF 导出工具手动将 Excel 工作表导出为 PDF 格式时,一切工作正常,但当我使用简单的 COM 自动化执行此操作时,除了包含迷你图的单元格之外,所有内容都导出为 PDF。

奇怪的是,当我向 Excel 工作表添加一些数据条时,数据条附近的迷你图突然被导出,但距离数据条较远的迷你图却没有导出。

我已经在多个不同的机器和操作系统上验证了这些问题。这可能与 StackOverflow 上的以下问题有关。

我正在使用以下非常直接的 VB.NET 代码。我尝试过使用各种设置和变量,但没有运气。

Public Class Form1

    Enum XlFixedFormatType
        xlTypePDF = 0
        xlTypeXPS = 1
    End Enum

    Enum XlUpdateLinks
        xlUpdateLinksUserSetting = 1
        xlUpdateLinksNever = 2
        xlUpdateLinksAlways = 3
    End Enum

    Enum XlFixedFormatQuality
        xlQualityStandard = 0
        xlQualityMinimum = 1
    End Enum

    Private Sub buttonConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonConvert.Click
        Dim pdf As String = Convert("C:\Sparkline.xlsx")
        Process.Start(pdf)
    End Sub

    Public Function Convert(ByVal fileName As String) As String
        Dim outPutFilename As String, printObject As Object = Nothing
        Dim app As Object      '** In reality this is a Microsoft.Office.Interop.Excel.Application
        Dim doc As Object      '** In reality this is a Microsoft.Office.Interop.Excel.Workbook

        app = CreateObject("Excel.Application")

        '** Open the _document
        doc = app.Workbooks.Open(fileName:=fileName, _
                                   UpdateLinks:=XlUpdateLinks.xlUpdateLinksNever, _
                                   ReadOnly:=True, _
                                   AddToMru:=False, _
                                   IgnoreReadOnlyRecommended:=True, _
                                   CorruptLoad:=True, _
                                   Editable:=False)

        '** Set visible sheets depending on selected range
        printObject = app.ActiveWorkbook.ActiveSheet

        '** Write the file under the same name, but with different extension
        outPutFilename = System.IO.Path.ChangeExtension(fileName, "pdf")

        printObject.ExportAsFixedFormat(Type:=XlFixedFormatType.xlTypePDF, _
                                fileName:=outPutFilename, _
                                quality:=XlFixedFormatQuality.xlQualityStandard, _
                                IncludeDocProperties:=False, _
                                IgnorePrintAreas:=False, _
                                OpenAfterPublish:=False)

        doc.Close(False)
        app.Quit()

        '** Return the name of the converted file
        Return outPutFilename

    End Function

End Class

I am having a problem programmatically exporting Excel sheets that contain sparklines to PDF format.

When I manually export the Excel sheet to PDF format using Excel 2010's native PDF Exporting facility then everything works fine, but the moment I do it using simple COM automation then everything is exported to PDF with the exception of cells containing sparklines.

The weird thing is that when I add a few data bars to the excel sheet then the sparklines near the data bars are suddenly exported, but the ones further away from the data bars are not.

I have verified these problems on multiple different machines and operating systems. This may be related to the following question on StackOverflow.

I am using the following, very straight forward, VB.NET code. I have tried playing around with the various settings and variables, but no luck.

Public Class Form1

    Enum XlFixedFormatType
        xlTypePDF = 0
        xlTypeXPS = 1
    End Enum

    Enum XlUpdateLinks
        xlUpdateLinksUserSetting = 1
        xlUpdateLinksNever = 2
        xlUpdateLinksAlways = 3
    End Enum

    Enum XlFixedFormatQuality
        xlQualityStandard = 0
        xlQualityMinimum = 1
    End Enum

    Private Sub buttonConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonConvert.Click
        Dim pdf As String = Convert("C:\Sparkline.xlsx")
        Process.Start(pdf)
    End Sub

    Public Function Convert(ByVal fileName As String) As String
        Dim outPutFilename As String, printObject As Object = Nothing
        Dim app As Object      '** In reality this is a Microsoft.Office.Interop.Excel.Application
        Dim doc As Object      '** In reality this is a Microsoft.Office.Interop.Excel.Workbook

        app = CreateObject("Excel.Application")

        '** Open the _document
        doc = app.Workbooks.Open(fileName:=fileName, _
                                   UpdateLinks:=XlUpdateLinks.xlUpdateLinksNever, _
                                   ReadOnly:=True, _
                                   AddToMru:=False, _
                                   IgnoreReadOnlyRecommended:=True, _
                                   CorruptLoad:=True, _
                                   Editable:=False)

        '** Set visible sheets depending on selected range
        printObject = app.ActiveWorkbook.ActiveSheet

        '** Write the file under the same name, but with different extension
        outPutFilename = System.IO.Path.ChangeExtension(fileName, "pdf")

        printObject.ExportAsFixedFormat(Type:=XlFixedFormatType.xlTypePDF, _
                                fileName:=outPutFilename, _
                                quality:=XlFixedFormatQuality.xlQualityStandard, _
                                IncludeDocProperties:=False, _
                                IgnorePrintAreas:=False, _
                                OpenAfterPublish:=False)

        doc.Close(False)
        app.Quit()

        '** Return the name of the converted file
        Return outPutFilename

    End Function

End Class

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

追我者格杀勿论 2024-10-09 13:23:37

我已与 Microsoft 支持联系,他们承认这是一个错误。产品团队正在考虑将其包含在未来的服务包中。

在此之前,在执行导出之前使应用程序可见即可解决问题。如果您不希望用户看到它,则可以将窗口放置在屏幕之外。

I have been in contact with Microsoft Support and they have acknowledged it is a bug. The product team is looking at it for inclusion in a future service pack.

Until then, making the Application Visible before carrying out the export will solve the problem. If you don't want the user to see it then you can position the window off screen.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文