vb6的导出到Excel的函数

 

vb6的导出到Excel的函数

Private Sub Command1_Click()
  Dim i As Integer
  Dim j As Integer
  Dim k As Integer
  Dim xlApp As Excel.Application
  Dim xlBook As Excel.Workbook
  Dim xlSheet As Excel.Worksheet
  Set xlApp = New Excel.Application
  Set xlBook = xlApp.Workbooks.Add
  Set xlSheet = xlBook.Worksheets(1)
   
  xlSheet.Columns.AutoFit
  Me.MousePointer = 11
   For k = 0 To DataGrid1.Columns.Count - 1 'DataGrid所有的列数
     xlSheet.Cells(1, k + 1) = DataGrid1.Columns(k).Caption '第一行为DataGrid的列标题
   Next
   DataGrid1.Scroll 0, -DataGrid1.FirstRow '导出前拉动过垂直滚动条,这个非常重要
   DataGrid1.Row = 0
   For i = 0 To DataGrid1.ApproxCount - 1 'DataGrid的所有行数

      For j = 0 To DataGrid1.Columns.Count - 1 'DataGrid所有的列数,若将此数改小到不拉DataGrid的垂直滚动条的时候能看见的行数的时候正常
         DataGrid1.Col = j
         xlSheet.Cells(i + 2, j + 1) = Adodc1.Recordset(j) 'DataGrid1.Text '从第二行显示'DataGrid的内容
      Next
     If i < DataGrid1.ApproxCount - 1 Then
       DataGrid1.Row = DataGrid1.Row + 1
     End If
   Next
  Me.MousePointer = 0
  MsgBox "导出成功!"
  xlApp.Visible = True
  Set xlApp = Nothing 'Excel 处于当前窗体
  Set xlBook = Nothing
  Set xlSheet = Nothing
End Sub 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值