从数据库取出资料用Excel显示的问题.在线等...

feilong215 2003-12-19 09:46:23
从数据库取出资料用Excel显示的问题.就是从a表取出数据 1, b表取出数据 2,3
1的栏位名为 TYPE , 2,3的栏位名为 PLACE,CD.这些资料用Excel表显示.我刚学vb,希望指点了.谢谢了.
...全文
26 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
hwyqy 2004-01-13
  • 打赏
  • 举报
回复
需引用Microsoft Excel 9.0 Object Library
hwyqy 2004-01-07
  • 打赏
  • 举报
回复
Up
xiaoxinghappy 2003-12-19
  • 打赏
  • 举报
回复
只要把 excel 文件当成数据源就行了,可以非常方便地把数据的内容导入到excel 的sheet里显示,代码如下:

Private Sub ExportToExcel(cntDataCn As Connection, byval FileName As String)
dim strSQL As String
strSQL = "select a.数据1 as TYPE,b.数据2 as PLACE,b.数据3 as CD INTO [Excel 8.0;Database=" & FileName & "].[exceltable] " & "from a,b where a.id=b.id"
''cntDataCn 是一个已经存在的连接,请自己连接好。
cntDataCn.Execute strSQL
End Sub
SoHo_Andy 2003-12-19
  • 打赏
  • 举报
回复
简单说就是使用ADO查询,得到一个数据集,然后通过

Set xlQuery = xlsheet.QueryTables.Add(Rs_Data, xlsheet.Range("a1"))

将数据集同你建立的Excel自动化对象关联起来,把数据导出到Excel中去
yoki 2003-12-19
  • 打赏
  • 举报
回复
这就是用ado呀,没看出来么?
hxy2003 2003-12-19
  • 打赏
  • 举报
回复
gz
feilong215 2003-12-19
  • 打赏
  • 举报
回复
我不一定要源码的,能解释一下听吗,我是用ado连接的.谢谢两位
feilong215 2003-12-19
  • 打赏
  • 举报
回复
我是用ado连接的啊,这样好像不行吧
yoki 2003-12-19
  • 打赏
  • 举报
回复


补充:
参数设置实例如下:
dim strOpen as string
strOpen="select a.数据1 as TYPE,b.数据2 as PLACE,b.数据3 as CD from a,b where a.id=b.id"
SoHo_Andy 2003-12-19
  • 打赏
  • 举报
回复

引用自小马哥

'导出记录集数据到Excel
'你设置好 strOpen 查询语句就可以了
Public Function ExporToExcel(strOpen As String)
'*********************************************************
'* 名称:ExporToExcel
'* 功能:导出数据到EXCEL
'* 用法:ExporToExcel(sql查询字符串)
'*********************************************************
Dim Rs_Data As New ADODB.Recordset
Dim Irowcount As Integer
Dim Icolcount As Integer
Dim cn As New ADODB.Connection
Dim xlapp As New Excel.Application
Dim xlbook As Excel.Workbook
Dim xlsheet As Excel.Worksheet
Dim xlQuery As Excel.QueryTable
With Rs_Data
If .State = adStateOpen Then
.Close
End If
.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\db1.mdb"
.CursorLocation = adUseClient
.CursorType = adOpenStatic
.Source = strOpen
.Open
End With
With Rs_Data
If .RecordCount < 1 Then
MsgBox ("没有记录!")
Exit Function
End If
'记录总数
Irowcount = .RecordCount
'字段总数
Icolcount = .Fields.Count
End With

Set xlapp = CreateObject("Excel.Application")
Set xlbook = Nothing
Set xlsheet = Nothing
Set xlbook = xlapp.Workbooks().Add
Set xlsheet = xlbook.Worksheets("sheet1")
xlapp.Visible = True

'添加查询语句,导入EXCEL数据
Set xlQuery = xlsheet.QueryTables.Add(Rs_Data, xlsheet.Range("a1"))

xlQuery.FieldNames = True '显示字段名
xlQuery.Refresh

xlapp.Application.Visible = True
Set xlapp = Nothing '"交还控制给Excel
Set xlbook = Nothing
Set xlsheet = Nothing

End Function

1,216

社区成员

发帖
与我相关
我的任务
社区描述
VB 数据库(包含打印,安装,报表)
社区管理员
  • 数据库(包含打印,安装,报表)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧