幸存者 橙光 攻略:EXCEL VBA透视表

来源:百度文库 编辑:偶看新闻 时间:2024/04/29 20:05:00
一般在搞透视表时,是先用录制宏的方法来实现的,当然可以再看下代码
Dim excel As Excel.Application
        Dim xBk As Excel._Workbook
        Dim xSt As Excel._Worksheet
        Dim xRange As Excel.Range
        Dim xPivotCache As Excel.PivotCache
        Dim xPivotTable As Excel.PivotTable
        Dim xPivotField As Excel.PivotField
        Dim cnnsr As String, sql As String
        Dim RowFields() As String = {"", "", ""}
        Dim PageFields() As String = {"", "", "", "", "", ""}

        'SERVER     是服务器名或服务器的IP地址
         'DATABASE 是数据库名
        'Table           是表名

        Try
             ' 开始导出
            cnnsr = "ODBC;DRIVER=SQL Server;SERVER=" + SERVER
            cnnsr = cnnsr + ";UID=;APP=Report Tools;WSID=ReportClient;DATABASE=" + DATABASE
            cnnsr = cnnsr + ";Trusted_Connection=Yes"

            excel = New Excel.ApplicationClass
            xBk = excel.Workbooks.Add(True)
            xSt = xBk.ActiveSheet

            xRange = xSt.Range("A4")
            xRange.Select()

            ' 开始
            xPivotCache = xBk.PivotCaches.Add(SourceType:=2)
            xPivotCache.Connection = cnnsr
            xPivotCache.CommandType = 2

             sql = "select * from " + Table

            xPivotCache.CommandText = sql
            xPivotTable = xPivotCache.CreatePivotTable(TableDestination:="Sheet1!R3C1", TableName:="数据透视表1", DefaultVersion:=1)

            '准备行字段
            RowFields(0) = "字段1"
            RowFields(1) = "字段2"
            RowFields(2) = "字段3"
            '准备页面字段
            PageFields(0) = "字段4"
            PageFields(1) = "字段5"
            PageFields(2) = "字段6"
            PageFields(3) = "字段7"
            PageFields(4) = "字段8"
            PageFields(5) = "字段9"
            xPivotTable.AddFields(RowFields:=RowFields, PageFields:=PageFields)

            xPivotField = xPivotTable.PivotFields("数量")
            xPivotField.Orientation = 4

             ' 关闭工具条
            'xBk.ShowPivotTableFieldList = False
            'excel.CommandBars("PivotTable").visible = False

            excel.Visible = True

        Catch ex As Exception
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            xBk.Close(0)
            excel.Quit()
            MessageBox.Show(ex.Message, "报表工具", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        End Try

又如:
PivotCaches.Add利用快取記憶體中快速運算建立一個樞紐分析表。須傳遞二個參數,如下:
SourceType:xlDatabase,Excel清單或資料庫。
xlExternal,外部資料庫。xlConsolidation,多種彙總資料範圍。 xlPivotTable,別的樞紐分析表。
SourceData:資料來源。
步驟2,指定資料來源為目前的工作表。
CreatePivotTable,參數如下:
TableDestination :必須參數,指定樞紐分析表的列印位置。
TableName :選擇性,樞紐分析表名稱。
步驟3,指定樞紐分析表列印位置,在CreatePivotTable的TableDestination 指定。
版面配置。指定每個區塊顯示的欄位。
PivotTables("Pivot1").AddFields,參數如下:
RowFields:指定列(R)區塊的欄位。
ColumnFields:指定欄(C)區塊對映的欄位。
PageFields:指定頁(P)區塊的欄位。

Sub Macro1()
' Macro1 巨集表
'
Range("A1").Select
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
"'依產品類別查詢銷售人員月銷售量'!R1C1:R356C6").CreatePivotTable TableDestination _
:=Range("H1"), TableName:="樞紐分析表1"
ActiveSheet.PivotTables("樞紐分析表1").SmallGrid = False
ActiveSheet.PivotTables("樞紐分析表1").AddFields RowFields:="銷售員", _
ColumnFields:="日期", PageFields:="產品類別"
ActiveSheet.PivotTables("樞紐分析表1").PivotFields("總計").Orientation = _
xlDataField
ActiveWindow.ScrollColumn = 7
Range("I13").Select
Range("I3").Select
Selection.Group Start:=True, End:=True, Periods:=Array(False, False, False, _
False, True, False, False)
ActiveWindow.ScrollColumn = 1
ActiveWindow.ScrollColumn = 12
ActiveWindow.SmallScroll ToRight:=-7
Range("I1").Select
ActiveSheet.PivotTables("樞紐分析表1").PivotFields("產品類別").CurrentPage = _
"糖果類"
End Sub