Python 如何从Excel2007数据透视表中检索sql代码
我有一个pivot表,我想通过python以编程方式重新创建它。有没有办法检索pivot表的当前sql代码以便在python中使用?我最近写了这篇文章。这可能对你有用。它尝试将活动工作簿的透视缓存和/或查询表数据复制到剪贴板。它被“下一步出错恢复”包围,因此如果找不到特定数据段,它将继续:Python 如何从Excel2007数据透视表中检索sql代码,python,excel,Python,Excel,我有一个pivot表,我想通过python以编程方式重新创建它。有没有办法检索pivot表的当前sql代码以便在python中使用?我最近写了这篇文章。这可能对你有用。它尝试将活动工作簿的透视缓存和/或查询表数据复制到剪贴板。它被“下一步出错恢复”包围,因此如果找不到特定数据段,它将继续: Sub Copy_Connection_Info_To_Clipboard() Dim ptCache As Excel.PivotCache Dim qtQueryTable As Excel.Query
Sub Copy_Connection_Info_To_Clipboard()
Dim ptCache As Excel.PivotCache
Dim qtQueryTable As Excel.QueryTable
Dim strPtCacheInfo As String
Dim strQueryTableInfo As String
Dim ws As Excel.Worksheet
Dim strConnectionInfo As String
Dim doConnectionInfo As DataObject
On Error Resume Next
For Each ptCache In ActiveWorkbook.PivotCaches
With ptCache
strPtCacheInfo = _
strPtCacheInfo _
& "PivotCache #" & "Index: " & .Index & vbCrLf & vbCrLf _
& "SourceDataFile: " & .SourceDataFile & vbCrLf & vbCrLf _
& "CommandText: " & .CommandText & vbCrLf & vbCrLf _
& "SourceConnectionFile: " & .SourceConnectionFile & vbCrLf & vbCrLf _
& "Connection: " & .Connection & vbCrLf & vbCrLf
End With
Next ptCache
If strPtCacheInfo <> "" Then
strPtCacheInfo = "PivotCache Info" & vbCrLf & vbCrLf & strPtCacheInfo
End If
For Each ws In ActiveWorkbook.Worksheets
If ws.QueryTables.Count > 0 Then
strQueryTableInfo = "Worksheet: " & ws.Name & vbCrLf
For Each qtQueryTable In ActiveSheet.QueryTables
With qtQueryTable
strQueryTableInfo = _
strQueryTableInfo _
& "QueryTable Name: " & .Name & vbCrLf & vbCrLf _
& .SourceDataFile & vbCrLf & vbCrLf _
& .CommandText & vbCrLf & vbCrLf _
& .SourceConnectionFile & vbCrLf & vbCrLf _
& .Connection & vbCrLf & vbCrLf
End With
Next qtQueryTable
End If
Next ws
If strQueryTableInfo <> "" Then
strQueryTableInfo = "Query Table Info" & vbCrLf & strQueryTableInfo
End If
strConnectionInfo = strPtCacheInfo & strQueryTableInfo
If strConnectionInfo <> "" Then
Set doConnectionInfo = New DataObject
doConnectionInfo.SetText strConnectionInfo
doConnectionInfo.PutInClipboard
End If
End Sub
将连接信息复制到剪贴板()
将ptCache设置为Excel.PivotCache
将qtQueryTable设置为Excel.QueryTable
将strPtCacheInfo设置为字符串
将strQueryTableInfo设置为字符串
将ws设置为Excel.Worksheet
作为字符串的Dim strConnectionInfo
Dim doConnectionInfo作为数据对象
出错时继续下一步
对于ActiveWorkbook.PivotCaches中的每个ptCache
使用ptCache
strPtCacheInfo=_
strPtCacheInfo_
&“数据透视缓存#”和“索引:&.Index&vbCrLf&vbCrLf”_
&“SourceDataFile:”&.SourceDataFile&vbCrLf&vbCrLf_
&“CommandText:&.CommandText&vbCrLf&vbCrLf_
&“SourceConnectionFile:&.SourceConnectionFile&vbCrLf&vbCrLf_
&“连接:&.连接&vbCrLf&vbCrLf
以
下一个ptCache
如果strPtCacheInfo“”则
strPtCacheInfo=“数据透视缓存信息”&vbCrLf&vbCrLf&strPtCacheInfo
如果结束
对于ActiveWorkbook.Worksheets中的每个ws
如果ws.QueryTables.Count>0,则
strQueryTableInfo=“工作表:”&ws.Name&vbCrLf
对于ActiveSheet.QueryTables中的每个qtQueryTable
带qtQueryTable
strQueryTableInfo=_
strQueryTableInfo_
&“查询表名称:”&.Name&vbCrLf&vbCrLf_
&.SourceDataFile&vbCrLf&vbCrLf_
&.CommandText&vbCrLf&vbCrLf_
&.SourceConnectionFile&vbCrLf&vbCrLf_
&.连接和vbCrLf和vbCrLf
以
下一个qtQueryTable
如果结束
下一个ws
如果strQueryTableInfo为“”,则
strQueryTableInfo=“查询表信息”&vbCrLf&strQueryTableInfo
如果结束
strConnectionInfo=strpCacheInfo&strQueryTableInfo
如果strConnectionInfo为“”,则
Set doConnectionInfo=新数据对象
doConnectionInfo.SetText strConnectionInfo
doConnectionInfo.PutInClipboard
如果结束
端接头