我在以编程方式将包含迷你图的 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