北京 | 上海 | 天津 | 重庆 | 广州 | 深圳 | 珠海 | 汕头 | 佛山 | 中山 | 东莞 | 南京 | 苏州 | 无锡 | 常州 | 南通 | 扬州 | 徐州 | 杭州 | 温州 | 宁波 | 台州 | 福州 | 厦门 | 泉州 | 龙岩 | 合肥 | 芜湖 | 成都 | 遂宁 | 长沙 | 株洲 | 湘潭 | 武汉 | 南昌 | 济南 | 青岛 | 烟台 | 潍坊 | 淄博 | 济宁 | 太原 | 郑州 | 石家庄 | 保定 | 唐山 | 西安 | 大连 | 沈阳 | 长春 | 昆明 | 兰州 | 哈尔滨 | 佳木斯 | 南宁 | 桂林 | 海口 | 贵阳 | 西宁 | 乌鲁木齐 | 包头 |
网上找了一段代码:如下
Public Sub ExportToExcel(ByVal dc As DynamiCubeLib.DCube) 'access 中
DynamiCubeLibCtl.DCube 'vb中
一下均为VB中代码,我直接拷到ACCESS中了。
Dim oXLApp As Object, cnt As Integer, iMaxRowDepth As Integer, iMaxColDepth As Integer
Dim iCol As Integer, iRow As Integer, iColDepth As Integer, iRowDepth As Integer
Dim xlCol As Integer, xlRow As Integer, iTempMaxColDepth As Integer, viscnt As Integer
On Error Resume Next
Screen.MousePointer = 11
If dc.ColCount > 256 Then
MsgBox "The view has to many columns for Excel to handle.", vbCritical
Screen.MousePointer = 0
Exit Sub
End If
' Creates OLE object to Excel
Set oXLApp = CreateObject("excel.application")
If Err Or (oXLApp Is Nothing) Then
Call MsgBox("Could not create excel application object. Please check Excel installation.", vbCritical, "Oops")
Exit Sub
End If
oXLApp.Workbooks.Add
oXLApp.Visible = True
' Find out MaxRowDepth
cnt = 0
Do While cnt < dc.RowCount
If dc.GetRowDepth(cnt) > iMaxRowDepth Then iMaxRowDepth = dc.GetRowDepth(cnt)
cnt = dc.GetNextRow(cnt)
Loop
' Find out MaxColDepth
iMaxColDepth = 0
cnt = 0
Do While cnt < dc.ColCount - 1
If iMaxColDepth < dc.GetColDepth(cnt) Then iMaxColDepth = dc.GetColDepth(cnt)
cnt = dc.GetNextCol(cnt)
Loop
' Export Column Headings; start at iMaxRowDepth
If MultipleVisDatas(dc) Then
iTempMaxColDepth = (iMaxColDepth + 1)
Else
iTempMaxColDepth = iMaxColDepth
End If
For iColDepth = 1 To (iTempMaxColDepth)
iCol = 0
xlCol = (iMaxRowDepth + 1)
Do While iCol < dc.ColCount
If MultipleVisDatas(dc) Then
If iColDepth < dc.GetColDepth(iCol) Then
oXLApp.Cells(iColDepth, xlCol).Value = dc.ColHeading(iCol, iColDepth)
End If
Else
If iColDepth <= dc.GetColDepth(iCol) Then
oXLApp.Cells(iColDepth, xlCol).Value = dc.ColHeading(iCol, iColDepth)
End If
End If
iCol = dc.GetNextCol(iCol)
xlCol = xlCol + 1
Loop
Next iColDepth
If MultipleVisDatas(dc) Then
' multiple visible datafields confirmed, now export captions
xlCol = (iMaxRowDepth + 1)
xlRow = (iMaxColDepth) ' Works for multi datas
iCol = 0
Do While iCol < dc.ColCount
oXLApp.Cells(xlRow, xlCol).Value = dc.ColHeading(iCol, (dc.ColFields.Count + 1))
iCol = dc.GetNextCol(iCol)
xlCol = (xlCol + 1)
Loop
End If
' Now export RowHeadings and Data
iRow = 0
xlRow = (iMaxColDepth + 1)
Do While iRow < dc.RowCount
' Get RowHeading(s)
xlCol = 1
For iRowDepth = 1 To dc.GetRowDepth(iRow)
oXLApp.Cells(xlRow, xlCol).Value = dc.RowHeading(iRow, iRowDepth)
xlCol = xlCol + 1
Next iRowDepth
' Get Data
iCol = 0
xlCol = (iMaxRowDepth + 1)
Do While iCol < dc.ColCount
oXLApp.Cells(xlRow, xlCol).Value = dc.DataValue(iRow, iCol)
iCol = dc.GetNextCol(iCol)
xlCol = (xlCol + 1)
Loop ' iCol
iRow = dc.GetNextRow(iRow)
xlCol = 1
xlRow = (xlRow + 1)
Loop ' iRow
Screen.MousePointer = 0
End Sub