打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
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

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
vb控制excel实现报表打印
关于 VFP 输出 EXCEL 的格式
VB.NET向Excel写入并保存数据
ADO把Recordset导入EXCEL后打印~ VB / 数据库(包含打印,安装,报表)...
VB 读取Excel表的内容 |VB 网|VB 视频教程|VB编程入门网
VB打开EXCEL的方法
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服