Excel Sparklines not exported to PDF when using COM Automation

我怕爱的太早我们不能终老 提交于 2019-12-13 02:13:08

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/4204830/excel-sparklines-not-exported-to-pdf-when-using-com-automation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!