Access導出Execl

golden8510 2009-10-06 12:39:43
VB+Access做的系統,
怎樣把數據庫Access內容導出execl?
...全文
212 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
golden8510 2009-10-17
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 jhone99 的回复:]
引用 13 楼 golden8510 的回复:


Data Input execl,
or Data output execl;


Data Input execl
[/Quote]
能否提供"Data output execl" VB Code;
TKS!
jhone99 2009-10-17
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 golden8510 的回复:]


Data Input execl,
or Data output execl;
[/Quote]

Data Input execl
golden8510 2009-10-16
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 gcome 的回复:]
我也在做这样的系统,该功能已经实现了。用的是ADO
将下面的函数,写进一个模块
Public Function OutputToExcel(strOpen As String)
    Dim Rs_Data As New ADODB.Recordset
    Dim Irowcount As Integer
    Dim Icolcount As Integer
   
    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 = cnn
        .CursorLocation = adUseClient
        .CursorType = adOpenStatic
        .LockType = adLockReadOnly
        .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"))
   
    With xlQuery
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = True
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
    End With
   
    xlQuery.FieldNames = True '显示字段名
    xlQuery.Refresh

    xlApp.Application.Visible = True
    Set xlApp = Nothing  '"交还控制给Excel
    Set xlBook = Nothing
    Set xlSheet = Nothing

End Function
函数参数说明 strOpen As String,strOpen是SQL查询语句
例如,定义数据集rst和链接cnn:
dim sql as string
sql="select * from 表1"
rst.Open sql,cnn,adOpenStatic, adLockPessimistic
OutputToExcel (sql)

这样就能将你的 表1 中的数据导入到Excel.
[/Quote]
Data Input execl,
or Data output execl;
Gcome 2009-10-14
  • 打赏
  • 举报
回复
我也在做这样的系统,该功能已经实现了。用的是ADO
将下面的函数,写进一个模块
Public Function OutputToExcel(strOpen As String)
Dim Rs_Data As New ADODB.Recordset
Dim Irowcount As Integer
Dim Icolcount As Integer

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 = cnn
.CursorLocation = adUseClient
.CursorType = adOpenStatic
.LockType = adLockReadOnly
.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"))

With xlQuery
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
End With

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

xlApp.Application.Visible = True
Set xlApp = Nothing '"交还控制给Excel
Set xlBook = Nothing
Set xlSheet = Nothing

End Function
函数参数说明 strOpen As String,strOpen是SQL查询语句
例如,定义数据集rst和链接cnn:
dim sql as string
sql="select * from 表1"
rst.Open sql,cnn,adOpenStatic, adLockPessimistic
OutputToExcel (sql)

这样就能将你的 表1 中的数据导入到Excel.
getemail 2009-10-11
  • 打赏
  • 举报
回复
这个是最好的
[Quote=引用 6 楼 citybird 的回复:]
4楼那个要装Access才能用,1楼那个用的是DAO,如果你用的是ADO还是用我这个吧
VB codedim connasnew adodb.connection
conn.open …………
conn.execute"select * into [excel 8.0;database=d:\123.xls].[导出结果] from tablename"
conn.closeset conn=nothing
[/Quote]
mnihjdok 2009-10-11
  • 打赏
  • 举报
回复
Dim xlApp As New Excel.Application
Dim xlBook As New Excel.Workbook
Dim xlSheet As New Excel.Worksheet
Dim conn As New ADODB.Connection
Dim rs As New Recordset

Set rs = ADOcn.Execute("select * from 入库单")

xlApp.Visible = False

Set xlBook = xlApp.Workbooks.Add


Set xlSheet = xlBook.Worksheets(1)

For i = 0 To rs.Fields.Count - 1


xlSheet.Cells(1, i + 1) = rs.Fields(i).Name


Next



xlSheet.Range("A2").CopyFromRecordset rs


xlApp.Dialogs(xlDialogSaveAs).Show (Format(Now, "yyyymmddhhmmss") & "入库单.xls")

xlBook.Close

xlApp.Quit

Set xlBook = Nothing

Set xlApp = Nothing

rs.Close
Set rs = Nothing
golden8510 2009-10-08
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 citybird 的回复:]
4楼那个要装Access才能用,1楼那个用的是DAO,如果你用的是ADO还是用我这个吧
VB codedim connasnew adodb.connection
conn.open …………
conn.execute"select * into [excel 8.0;database=d:\123.xls].[导出结果] from tablename"
conn.closeset conn=nothing
[/Quote]
我有安裝Access;
golden8510 2009-10-08
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 veron_04 的回复:]
问题出在什么地方?错误提示是什么?
[/Quote]
以下該句;

Set accP = GetObject(strSourcePath, "Access.Application")

三楼の郎 2009-10-08
  • 打赏
  • 举报
回复
4楼那个要装Access才能用,1楼那个用的是DAO,如果你用的是ADO还是用我这个吧

dim conn as new adodb.connection
conn.open …………
conn.execute "select * into [excel 8.0;database=d:\123.xls].[导出结果] from tablename"
conn.close
set conn=nothing
贝隆 2009-10-08
  • 打赏
  • 举报
回复
问题出在什么地方?错误提示是什么?
golden8510 2009-10-07
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 veron_04 的回复:]
http://download.csdn.net/source/1483928
[/Quote]
有問題,
問題如下:


'使用须知:1、添加对:Microsoft Access 11.0 Object Library的引用
Option Explicit
Dim accP As Access.Application
Dim strSourcePath As String
Dim strReportPath As String
Dim strObjectName As String
Private Sub Command1_Click()
With cdlP
.DialogTitle = "数据转换"
.InitDir = App.Path & "\数据文件\"
.Filter = "数据文件 (*.mdb)|*.mdb"
.ShowOpen
strSourcePath = .FileName
End With
strObjectName = "TB_Team" '注意,这个名称必须和你选中的数据库中要转换的表格名称一样
strReportPath = App.Path & "\11.xls" '要生成的文件名
If strSourcePath <> "" Then
Set accP = GetObject(strSourcePath, "Access.Application")
accP.DoCmd.OutputTo acOutputTable, strObjectName, acFormatXLS, strReportPath
accP.CloseCurrentDatabase
Set accP = Nothing
End If
End Sub
golden8510 2009-10-07
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 veron_04 的回复:]
http://download.csdn.net/source/1483928
[/Quote]
TKS!
贝隆 2009-10-06
  • 打赏
  • 举报
回复
clear_zero 2009-10-06
  • 打赏
  • 举报
回复
现在没东西试,从网上找了段代码你看看

Private Sub ExportOneTable()

'EXPORTS TABLE IN ACCESS DATABASE TO EXCEL
'REFERENCE TO DAO IS REQUIRED

Dim strExcelFile As String
Dim strWorksheet As String
Dim strDB As String
Dim strTable As String
Dim objDB As Database

'Change Based on your needs, or use
'as parameters to the sub
strExcelFile = "C:\My Documents\MySpreadSheet.xls"
strWorksheet = "WorkSheet1"
strDB = "C:\My Documents\MyDatabase.mdb"
strTable = "MyTable"

Set objDB = OpenDatabase(strDB)

'If excel file already exists, you can delete it here
If Dir(strExcelFile) <> "" Then Kill strExcelFile

objDB.Execute _
"SELECT * INTO [Excel 8.0;DATABASE=" & strExcelFile & _
"].[" & strWorksheet & "] FROM " & "[" & strTable & "]"
objDB.Close
Set objDB = Nothing

End Sub


1,216

社区成员

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

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