WPS 加载项开发 > 表格 API 参考 > PivotCache > 方法 > PivotCache.CreatePivotTable 方法
创建一个基于 PivotCache 对象的数据透视表。返回一个 PivotTable 对象。

语法

表达式.CreatePivotTable(TableDestination, TableName, ReadData, DefaultVersion)

表达式   一个代表 PivotCache 对象的变量。

参数

名称必选/可选数据类型说明
TableDestination必选Variant数据透视表目标区域(工作表中用于放置所生成的数据透视表的区域)左上角的单元格。目标区域必须位于工作簿(此工作簿包含由 expression 指定的 PivotCache 对象)的某个工作表中。
TableName可选Variant新的数据透视表的名称。
ReadData可选Variant如果该值为 True,则创建一个包含外部数据库中所有记录的数据透视表高速缓存;此高速缓存可以很大。如果为 False,则允许在实际读取数据之前将某些字段设置为基于服务器的页字段。
DefaultVersion可选Variant数据透视表的默认版本。

返回值
PivotTable

说明

有关创建基于数据透视表高速缓存的数据透视表的另一种方法,请参阅 PivotTables 对象的 Add 方法。

示例

本示例在活动工作表的 A3 单元格上新建一个基于 OLAP 提供程序?(OLAP 提供程序:对特定类型的 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


请参阅