|
数据透视表的外部数据可以直接连到sybase数据库吗?
用odbc这种方式的宏。Sub Macro1()
With ActiveWorkbook.PivotCaches.Add(SourceType:=xlExternal)
.Connection = _
"ODBC;DSN=wz_shenbao;NA=*.*.*.23,5000;DB=database;UID=operator;"
.CommandType = xlCmdSql
.CommandText = Array( _
"SELECT SJYDB.NSHDM, SJYDB.SZ, SJYDB.SM, SJYDB.RKS, SJYDB.SBSJ" & Chr(13) & "" & Chr(10) & "FROM ShenBao.dbo.SJYDB SJYDB" & Chr(13) & "" & Chr(10) & "WHERE (SJYDB.NSHDM='39000125') AND (SJYDB.ZFBZ='N')" & Chr(13) & "" & Chr(10) & "ORDER BY SJYDB.SBSJ" _
)
.CreatePivotTable TableDestination:="[Book1]Sheet1!R1C1", TableName:= _
"数据透视表1", DefaultVersion:=xlPivotTableVersion10
End With
With ActiveSheet.PivotTables("数据透视表1").PivotFields("NSHDM")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("数据透视表1").AddDataField ActiveSheet.PivotTables("数据透视表1" _
).PivotFields("RKS"), "求和项:RKS", xlSum
With ActiveSheet.PivotTables("数据透视表1").PivotFields("SZ")
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.PivotTables("数据透视表1").PivotCache.Refresh
End Sub
不用odbc的话,要怎么写? |
|