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

📄 form1.frm

📁 VB中如何正确导出DataGrid的数据
💻 FRM
字号:
VERSION 5.00
Object = "{67397AA1-7FB1-11D0-B148-00A0C922E820}#6.0#0"; "MSADODC.OCX"
Object = "{CDE57A40-8B86-11D0-B3C6-00A0C90AEA82}#1.0#0"; "MSDATGRD.OCX"
Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "MSCOMCTL.OCX"
Begin VB.Form Form1 
   Caption         =   "Form1"
   ClientHeight    =   4680
   ClientLeft      =   60
   ClientTop       =   450
   ClientWidth     =   6345
   LinkTopic       =   "Form1"
   ScaleHeight     =   4680
   ScaleWidth      =   6345
   StartUpPosition =   3  '窗口缺省
   Begin MSComctlLib.ProgressBar ProgressBar1 
      Height          =   375
      Left            =   3840
      TabIndex        =   2
      Top             =   960
      Width           =   2415
      _ExtentX        =   4260
      _ExtentY        =   661
      _Version        =   393216
      Appearance      =   0
      Scrolling       =   1
   End
   Begin VB.CommandButton Command1 
      Caption         =   "导出"
      Height          =   375
      Left            =   3960
      TabIndex        =   1
      Top             =   2160
      Width           =   975
   End
   Begin MSDataGridLib.DataGrid DataGrid1 
      Bindings        =   "Form1.frx":0000
      Height          =   3975
      Left            =   0
      TabIndex        =   0
      Top             =   120
      Width           =   3735
      _ExtentX        =   6588
      _ExtentY        =   7011
      _Version        =   393216
      HeadLines       =   1
      RowHeight       =   15
      BeginProperty HeadFont {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
         Name            =   "宋体"
         Size            =   9
         Charset         =   134
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
         Name            =   "宋体"
         Size            =   9
         Charset         =   134
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      ColumnCount     =   2
      BeginProperty Column00 
         DataField       =   ""
         Caption         =   ""
         BeginProperty DataFormat {6D835690-900B-11D0-9484-00A0C91110ED} 
            Type            =   0
            Format          =   ""
            HaveTrueFalseNull=   0
            FirstDayOfWeek  =   0
            FirstWeekOfYear =   0
            LCID            =   2052
            SubFormatType   =   0
         EndProperty
      EndProperty
      BeginProperty Column01 
         DataField       =   ""
         Caption         =   ""
         BeginProperty DataFormat {6D835690-900B-11D0-9484-00A0C91110ED} 
            Type            =   0
            Format          =   ""
            HaveTrueFalseNull=   0
            FirstDayOfWeek  =   0
            FirstWeekOfYear =   0
            LCID            =   2052
            SubFormatType   =   0
         EndProperty
      EndProperty
      SplitCount      =   1
      BeginProperty Split0 
         BeginProperty Column00 
         EndProperty
         BeginProperty Column01 
         EndProperty
      EndProperty
   End
   Begin MSAdodcLib.Adodc Adodc1 
      Height          =   375
      Left            =   120
      Top             =   4200
      Width           =   3615
      _ExtentX        =   6376
      _ExtentY        =   661
      ConnectMode     =   0
      CursorLocation  =   3
      IsolationLevel  =   -1
      ConnectionTimeout=   15
      CommandTimeout  =   30
      CursorType      =   3
      LockType        =   3
      CommandType     =   2
      CursorOptions   =   0
      CacheSize       =   50
      MaxRecords      =   0
      BOFAction       =   0
      EOFAction       =   0
      ConnectStringType=   1
      Appearance      =   1
      BackColor       =   -2147483643
      ForeColor       =   -2147483640
      Orientation     =   0
      Enabled         =   -1
      Connect         =   "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Test.mdb;Persist Security Info=False"
      OLEDBString     =   "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Test.mdb;Persist Security Info=False"
      OLEDBFile       =   ""
      DataSourceName  =   ""
      OtherAttributes =   ""
      UserName        =   ""
      Password        =   ""
      RecordSource    =   "TestTable"
      Caption         =   "Adodc1"
      BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
         Name            =   "宋体"
         Size            =   9
         Charset         =   134
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      _Version        =   393216
   End
   Begin VB.Label Label1 
      Caption         =   "Label1"
      Height          =   375
      Left            =   3840
      TabIndex        =   3
      Top             =   1440
      Width           =   2415
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'要实现这个功能,必须要在VB中引用Microsoft Excel 10.0 Objects Library,其中10.0是
'版本号,各个Office版本不一样,在程序中并非要求一致。
Private Sub Command1_Click()
Dim i As Integer
Dim j As Integer
Dim k As Integer 'DataGrid的列数
Dim Icolcount As Integer
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
i = 0
j = 0
k = 0

DataGrid1.Scroll 0, -DataGrid1.FirstRow '定位到第一行
DataGrid1.Row = 0

ProgressBar1.Max = DataGrid1.ApproxCount - 1 '进度条
ProgressBar1.Visible = True

Set xlApp = CreateObject("Excel.Application")

Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets(1)
xlSheet.Columns.AutoFit
 

For k = 0 To DataGrid1.Columns.Count - 1 'DataGrid所有的列数
    xlSheet.Cells(1, k + 1) = DataGrid1.Columns(k).Caption '第一行为DataGrid的列标题
Next

Icolcount = DataGrid1.Columns.Count

For i = 0 To DataGrid1.ApproxCount - 1 'DataGrid的所有行数
    DoEvents
    Label1.Caption = "正在导出第" & i & "条记录"
    For j = 0 To DataGrid1.Columns.Count - 1 'DataGrid所有的列数,若将此数改小到不拉DataGrid的垂直滚动条的时候能看见的行数的时候正常
        DataGrid1.Col = j
        xlSheet.Cells(i + 2, j + 1) = DataGrid1.Text '从第二行显示'DataGrid的内容
    Next
    If i < DataGrid1.ApproxCount - 1 Then
        DataGrid1.Row = DataGrid1.Row + 1
        
        If ProgressBar1.Value <= ProgressBar1.Max - 1 Then
            ProgressBar1.Value = ProgressBar1.Value + 1
            
        Else
            ProgressBar1.Visible = False
        End If
    End If
Next

With xlSheet '设置导出后的外观显示
        .Range(.Cells(1, 1), .Cells(1, Icolcount)).Font.Name = "黑体"
        '设标题为黑体字
        .Range(.Cells(1, 1), .Cells(1, Icolcount)).Font.Bold = True
        '标题字体加粗
        .Range(.Cells(1, 1), .Cells(Irowcount + 1, Icolcount)).Borders.LineStyle = xlContinuous
        '设表格边框样式
End With
    
With xlSheet.PageSetup '设置页眉页脚,根据需要自己修改.但修改时应注意格式,在可修改的地方修改
        .LeftHeader = "" & Chr(10) & "&""楷体_GB2312,常规""&10公司名称:" & "美国微软信息有限公司"
        .CenterHeader = "&""楷体_GB2312,常规""美国微软信息有限公司员工加班考勤情况表&""宋体,常规""" & Chr(10) & "&""楷体_GB2312,常规""&10日 期:" & Date 'Date为当前日期
        .RightHeader = "" & Chr(10) & "&""楷体_GB2312,常规""&10单位:" & "后勤部门"
        .LeftFooter = "&""楷体_GB2312,常规""&10制表人:" & "陈延涛"
        .CenterFooter = "&""楷体_GB2312,常规""&10制表日期:" & Date
        .RightFooter = "&""楷体_GB2312,常规""&10第&P页 共&N页"
        .PrintTitleRows = "$1:$1"
End With
    
Label1.Caption = "导出完成!"
'Me.MousePointer = 0
MsgBox "导出完成!", vbOKOnly + vbInformation, "恭喜"
ProgressBar1.Visible = False
xlApp.Visible = True
Set xlApp = Nothing  '交还控制给Excel
Set xlBook = Nothing
Set xlSheet = Nothing

End Sub

⌨️ 快捷键说明

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