⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.frm

📁 vb精彩编程希望大家有用
💻 FRM
字号:
VERSION 5.00
Begin VB.Form Form1 
   Caption         =   "Excel To MDB"
   ClientHeight    =   2100
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   5760
   LinkTopic       =   "Form1"
   ScaleHeight     =   2100
   ScaleWidth      =   5760
   StartUpPosition =   3  '窗口缺省
   Begin VB.TextBox txtAccessFile 
      Height          =   285
      Left            =   1560
      TabIndex        =   3
      Top             =   720
      Width           =   4095
   End
   Begin VB.CommandButton cmdLoad 
      Caption         =   "转换数据"
      Default         =   -1  'True
      Height          =   495
      Left            =   2160
      TabIndex        =   2
      Top             =   1440
      Width           =   1215
   End
   Begin VB.TextBox txtExcelFile 
      Height          =   285
      Left            =   1560
      TabIndex        =   1
      Top             =   240
      Width           =   4095
   End
   Begin VB.Label Label1 
      Caption         =   "Access数据库:"
      Height          =   255
      Index           =   1
      Left            =   120
      TabIndex        =   4
      Top             =   735
      Width           =   1335
   End
   Begin VB.Label Label1 
      Caption         =   "Excel文件:"
      Height          =   255
      Index           =   0
      Left            =   120
      TabIndex        =   0
      Top             =   255
      Width           =   1335
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private Sub cmdLoad_Click()
    Dim excel_app As Object
    Dim excel_sheet As Object
    Dim db As Database
    Dim new_value As String
    Dim row As Integer
    Screen.MousePointer = vbHourglass
    DoEvents
    ' 建立Excel对象
    Set excel_app = CreateObject("Excel.Application")
    '打开Excel工作薄.
    excel_app.Workbooks.Open FileName:=txtExcelFile.Text
    ' 检查Excel的版本
    If Val(excel_app.Application.Version) >= 8 Then
        Set excel_sheet = excel_app.ActiveSheet
    Else
        Set excel_sheet = excel_app
    End If
    ' 打开数据库
    Set db = OpenDatabase(txtAccessFile.Text)
    '取得Excel表中单元格的值,然后插入数据库的TestValues表中
    row = 1
    Do
        '取得表中单元格的值
        new_value = Trim$(excel_sheet.Cells(row, 1))
        If Len(new_value) = 0 Then Exit Do
        db.Execute "INSERT INTO TestValues VALUES (" & new_value & ")"
        row = row + 1
    Loop
    '关闭数据库
    db.Close
    Set db = Nothing
    '退出Excel
    excel_app.ActiveWorkbook.Close False
    excel_app.Quit
    Set excel_sheet = Nothing
    Set excel_app = Nothing
    Screen.MousePointer = vbDefault
    MsgBox "复制了 " & Format$(row - 1) & " 个值到数据库中。"
End Sub
Private Sub Form_Load()
    Dim file_path As String
    file_path = App.Path
    If Right$(file_path, 1) <> "\" Then file_path = file_path & "\"
    txtExcelFile.Text = file_path & "XlsToMdb.xls"
    txtAccessFile.Text = file_path & "XlsToMdb.mdb"
End Sub


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -