zoukankan      html  css  js  c++  java
  • VB开发——Access数据库数据转化到Excell表格

    个人认为分为三种情况:

    整个数据库转化为表格,从数据库选择部分数据转化为表格,从特定记录中选择数据转化为表格

    1)整个数据库转化为表格

    Private Sub Command1_Click()
         Dim acApp As Access.Application
         Dim strReportName As String
         Dim strReportPath As String
        
         Const SAMPLE_DB_PATH As String = "c:/EastRiver.mdb"
        
         strReportName = "akaoqin "
         strReportPath = "c:/"
         ' Start Access and open Northwind Traders database.
         Set acApp = GetObject(SAMPLE_DB_PATH, "Access.Application")
         acApp.DoCmd.OutputTo acOutputQuery, strReportName, _
         acFormatXLS, strReportPath & "autoxls.xls", True
        
    End Sub
        

        应该是效率最高的。TransferDatabase这条命令也能完成这个功能,用法一样,大家可以试试。

    2)从数据库选择部分数据转化为表格

        VB支持SQL语言的SELECT ... INTO语句,这使得你可以将一种数据库轻松地转换为另一种格式,也可以在同一种格式的数据库中进行转换。下面我们以将.MDB格式转换为Excel形式为例。
        首先,打开.MDB文件。如
        Dim dbSource As Database
        
        Set dbSource = OpenDatabase("MY.MDB")
        然后使用SELECT ... INTO语句转换文件。
        dbSource.Execute("SELECT * INTO my IN 'c:/documents/xldata.xls' 'EXCEL 5.0;' FROM table1")
        这里,IN子句后面是转换后的数据库文件名,'EXCEL 5.0;'表示Excel 5.0/95格式,也可以是其他VB支持的格式。
        SELECT ... INTO建立新的表或数据库,而如果要将数据追加到已经存在的数据库中,可以使用INSERT ... INTO语句。

    3)从特定记录中选择数据转化为表格

       一个将Access数据库转换为Excel的例子:
         Dim xlApp As Excel.Application
         Dim xlBook As Excel.Workbook
         Dim xlSheet As Excel.Worksheet
         Dim oldPointer As Integer 'used to save the current
         Dim rPointer As Integer 'current saving record
         Dim xlsPointer As Integer 'used to point out the position the current record saving
        
         oldPointer = rs_com.Bookmark 'save current pointer of rs_com
         xlsPointer = 2 'the first line of Excel sheet
         Set xlApp = CreateObject("Excel.Application") 'create Excel application
         Set xlBook = xlApp.Workbooks.Add
         Set xlSheet = xlBook.Worksheets(1)
         Hide
         frmSaveProgress.Show 'show the progress status
         rs_com.MoveFirst 'rs_com is a table of your database
         Dim i As Integer
         While Not rs_com.EOF 'save ecah record to Excel file
         For i = 0 To x 'x =记录数
         xlSheet.Cells(xlsPointer, i + 1) = rs_com.Fields(i)
         Next
         frmSaveProgress.nextStep (rs_com.Bookmark / rs_com.RecordCount)
         rs_com.MoveNext
         xlsPointer = xlsPointer + 1
         Wend
         Unload frmSaveProgress 'end save to Excel file
         frmDataOper.Show
         xlBook.SaveAs (xlsFileName)
         xlBook.Close
         xlApp.Quit

    另一种实现


    Set xlApp = New Excel.Application

    Set xlApp = CreateObject("Excel.Application")
    '激活EXCEL应用程序
    xlApp.Visible = False
    '隐藏EXCEL应用程序窗口
    Set xlBook = xlApp.Workbooks.Open(strDestination)
    '打开工作簿,strDestination为一个EXCEL报表文件
    Set xlsheet = xlBook.Worksheets(1)
    '设定工作表

    datPrimaryRS.Recordset.MoveFirst
    'datPrimaryRS为Data控件
        If IsNull(datPrimaryRS.Recordset!姓名) = False Then
        xlSheet.Cells(4, 2) = datPrimaryRS.Recordset!姓名
        End If
        If IsNull(datPrimaryRS.Recordset!性别) = False Then
        xlSheet.Cells(4, 4) = datPrimaryRS.Recordset!性别
        End If
        If IsNull(datPrimaryRS.Recordset!民族) = False Then
        xlSheet.Cells(4, 6) = datPrimaryRS.Recordset!民族
        End If

    注意事项:

      1、在VB工程中添加对Excel类型库的引用

      为了能从VB应用程序中访问Excel丰富的内部资源,使Excel应用程序运行得更快,需要在VB工程中添加对Excel类型库的引用。具体步骤如下:

      a)从VB5“工程”菜单中选择“引用”;

      b) 在“引用”对话框中选择Excel类型库:"Microsoft Excel9.0 Object Library";

      c)单击左边小方框,使之出现“√”符号;

      d)按“确定”退出。

      注:要想在VB应用程序中调用Excel,你的计算机系统中必须安装Excel。

    Access也是类似的,没有这步操作,是无法编译通过的!

  • 相关阅读:
    ApplicationContext之getBean方法详解
    Windows10终端优化方案:Ubuntu子系统+cmder+oh-my-zsh
    向 Windows 高级用户进阶,这 10 款效率工具帮你开路 | 新手问号
    Ditto —— windows 剪贴板增强小工具(复制粘贴多条记录)
    Service Mesh服务网格:是什么和为什么
    正确理解Spring事务和数据库事务和锁
    Spring中@Transactional事务回滚(含实例详细讲解,附源码)
    五分钟搞清楚MySQL事务隔离级别
    事务并发的问题场景图解
    Spring的事务管理和数据库事务相关知识
  • 原文地址:https://www.cnblogs.com/ainima/p/6331607.html
Copyright © 2011-2022 走看看