有几种方法可以使用通常作为 MS Office 的一部分安装的内容。您选择哪个在某种程度上取决于您正在编写的 Office 版本。
鉴于大多数客户对与 Excel/PPT 等提供的默认图表不匹配的图表有非常具体的要求,您要么需要插入一个图表,然后在程序控制下进行所有格式化,要么我的偏好是存储 pre - 单独的 PPT/PPTX 文件或文件中的格式化样本图表。您可以不可见地打开它,根据需要将图表复制/粘贴到当前幻灯片中,然后以编程方式修改数据。
这样做的一大优势是您(或客户,如果您选择让他们参与其中)可以快速轻松地更改格式,而无需更改一行代码。
回到你最初的问题,原生 PPT 图表中的数据很容易访问。一个快速的 VBA 示例:
Sub FiddleTheChartData()
Dim oSh As Shape
Dim oCht As Chart
Dim oChtData As ChartData
Dim x As Long
' For purposes of example, assume that we've got a chart in PPT
' and that it's selected:
Set oSh = ActiveWindow.Selection.ShapeRange(1)
' We could as easily pass a reference to the shape to this routine as a param
' Sanity test; if it's not a chart, bug out
If Not oSh.HasChart Then
Exit Sub
Else
Set oCht = oSh.Chart
Set oChtData = oCht.ChartData
End If
With oChtData
.Activate
.Workbook.Worksheets("Sheet1").Cells(3, 3) = 42
End With
' This leaves the workbook open in Excel, so:
oChtData.Workbook.Close
' Might want to iterate through the Workbook.Parent's worksheets and
' if there are more than one, decide whether or not to close them and dismiss
' Excel or not
End Sub