Excel Sparklines not exported to PDF when using COM Automation
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:=Fals开发者_JS百科e, _
OpenAfterPublish:=False)
doc.Close(False)
app.Quit()
'** Return the name of the converted file
Return outPutFilename
End Function
End Class
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.
精彩评论