vb导出到excel的方法

//导出

Private Sub Command5_Click()

If Adodc2.Recordset.RecordCount = 0 Then
 MsgBox "没有数据可导出!", vbExclamation, "导出"
Else

MsgBox "将把数据导出到EXCLE里,请稍等.......", vbExclamation, "导出"
Screen.MousePointer = vbHourglass
Dim i As Integer
Dim j 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.Cells.Columns.  '.AutoFit
    Screen.MousePointer = 11
    With xlSheet
            'EXCLE第一列的宽度
            .Columns(1).ColumnWidth = 9.15
            .Columns(2).ColumnWidth = 14.13
            .Columns(3).ColumnWidth = 14.63
            .Columns(4).ColumnWidth = 6.5
            .Columns(5).ColumnWidth = 12.5
            '字体 边框  居中
            .Cells.Font.Size = 9
            .Cells(1, 1).Borders.LineStyle = 1 '边框
            .Cells(1, 2).Borders.LineStyle = 1
            .Cells(1, 3).Borders.LineStyle = 1
            .Cells(1, 4).Borders.LineStyle = 1
            .Cells(1, 5).Borders.LineStyle = 1
            .Cells.HorizontalAlignment = xlCenter
             
            .Cells.WrapText = True  '自动换行
            .Cells.EntireColumn.AutoFit  '行高根据内容自动调整
            .Cells.EntireRow.AutoFit
           
            '文件名称
            .name = CStr(Date)  '时间为名称
           
            '标题列名称
            .Cells(1, 1) = "t1"
            .Cells(1, 2) = "t2"
            .Cells(1, 3) = "t3"
            .Cells(1, 4) = "t4"
            .Cells(1, 5) = "t5"
    End With
   

 For i = 1 To VSFlexGrid5.Rows - 1 '遍历VSFlexGrid5的所有行数
    For j = 1 To VSFlexGrid5.Cols - 1 '遍历VSFlexGrid5所有的列数

             str = Trim(VSFlexGrid5.TextMatrix(i, j))

             '去掉空格 回车符
             str = Replace(str, vbCr, "")
             str = Replace(str, vbLf, "")
             xlSheet.Cells(i + 1, j) = Trim(str)
            '加边框
            xlSheet.Cells(i + 1, j).Borders.LineStyle = 1
            '内容靠底部
            xlSheet.Range(xlSheet.Cells(i + 1, j), xlSheet.Cells(i + 1, j)).VerticalAlignment = 3
            xlSheet.Range(xlSheet.Cells(i + 1, j), xlSheet.Cells(i + 1, j)).HorizontalAlignment = 3
    Next
 Next
Screen.MousePointer = 0
MsgBox "数据已经成功导出!", vbExclamation, "导出"
xlApp.Visible = True
Set xlApp = Nothing 'Excel 处于当前窗体
Set xlBook = Nothing
Set xlSheet = Nothing

End If
End Sub

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值