vb导出到excel的方法

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

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

相关文档
最新文档