创建一个基于 PivotCache 对象的数据透视表。返回一个 PivotTable 对象。
expression.CreatePivotTable(TableDestination, TableName, ReadData, DefaultVersion)
expression 该表达式返回一个 PivotCache 对象。
TableDestination Variant 类型,必需。代表数据透视表目标区域(工作表中用于放置所生成的数据透视表的区域)左上角的单元格。目标区域必须位于工作簿(此工作簿应包含由 expression 指定的 PivotCache 对象)的某个工作表中。
TableName Variant 类型,可选。新的数据透视表的名称。
ReadData Variant 类型,可选。如果该值为 True,则创建一个包含外部数据库中所有记录的数据透视表高速缓存;此高速缓存可以很大。如果该值为 False,则允许在实际读取数据前将有些字段设置为基于服务器的页字段。
DefaultVersion Variant 类型,可选。数据透视表的默认版本。
有关创建基于数据透视表高速缓存的数据透视表的另一种方法,请参阅 PivotTable 对象的 Add 方法。
本示例在活动工作表的 A3 单元格上新建一个基于 OLAP 供应者的数据透视表高速缓存,然后基于该高速缓存新建一个数据透视表。
With ActiveWorkbook.PivotCaches.Add(SourceType:=xlExternal)
.Connection = _
"OLEDB;Provider=MSOLAP;Location=srvdata;Initial Catalog=National"
.CommandType = xlCmdCube
.CommandText = Array("Sales")
.MaintainConnection = True
.CreatePivotTable TableDestination:=Range("A3"), _
TableName:= "PivotTable1"
End With
With ActiveSheet.PivotTables("PivotTable1")
.SmallGrid = False
.PivotCache.RefreshPeriod = 0
With .CubeFields("[state]")
.Orientation = xlColumnField
.Position = 1
End With
With .CubeFields("[Measures].[Count Of au_id]")
.Orientation = xlDataField
.Position = 1
End With
End With
本示例在活动工作表的 A3 单元格上,通过连接到 Microsoft Jet 上的 ADO 创建一个新的数据透视表高速缓存,然后再基于该高速缓存新建一个数据透视表。
Dim cnnConn As ADODB.Connection
Dim rstRecordset As ADODB.Recordset
Dim cmdCommand As ADODB.Command
' Open the connection.
Set cnnConn = New ADODB.Connection
With cnnConn
.ConnectionString = _
"Provider=Microsoft.Jet.OLEDB.4.0"
.Open "C:\perfdate\record.mdb"
End With
' Set the command text.
Set cmdCommand = New ADODB.Command
Set cmdCommand.ActiveConnection = cnnConn
With cmdCommand
.CommandText = "Select Speed, Pressure, Time From DynoRun"
.CommandType = adCmdText
.Execute
End With
' Open the recordset.
Set rstRecordset = New ADODB.Recordset
Set rstRecordset.ActiveConnection = cnnConn
rstRecordset.Open cmdCommand
' Create a PivotTable cache and report.
Set objPivotCache = ActiveWorkbook.PivotCaches.Add( _
SourceType:=xlExternal)
Set objPivotCache.Recordset = rstRecordset
With objPivotCache
.CreatePivotTable TableDestination:=Range("A3"), _
TableName:="Performance"
End With
With ActiveSheet.PivotTables("Performance")
.SmallGrid = False
With .PivotFields("Pressure")
.Orientation = xlRowField
.Position = 1
End With
With .PivotFields("Speed")
.Orientation = xlColumnField
.Position = 1
End With
With .PivotFields("Time")
.Orientation = xlDataField
.Position = 1
End With
End With
' Close the connections and clean up.
cnnConn.Close
Set cmdCommand = Nothing
Set rstRecordSet = Nothing
Set cnnConn = Nothing