VB中怎么把DataGrid1的查询出的结果连同标题一起导出
展开全部
导出到电子表格Excel
Private Sub Command1_Click()
Dim K As Integer
Set oExcel = CreateObject("Excel.Application")
Set oBook = oExcel.Workbooks.Add()
With oBook.Worksheets(1)
.Columns("A:A").ColumnWidth = 13.88
.Columns("B:B").ColumnWidth = 5.75
.Columns("C:C").ColumnWidth = 7.5
.cells(4, 1) = Me.DataGrid1.Columns(0).Caption
.cells(4, 2) = Me.DataGrid1.Columns(1).Caption
.cells(4, 3) = Me.DataGrid1.Columns(2).Caption
For K = 0 To Me.DataGrid1.VisibleRows - 1
Me.DataGrid1.Row = K '这一步是关键
.cells(K + 5, 1) = Me.DataGrid1.Columns(0)
.cells(K + 5, 2) = Me.DataGrid1.Columns(1)
.cells(K + 5, 3) = Me.DataGrid1.Columns(2)
Next K
End With
oExcel.Visible = True
Set oBook = Nothing
Set oExcel = Nothing
end sub
Private Sub Command1_Click()
Dim K As Integer
Set oExcel = CreateObject("Excel.Application")
Set oBook = oExcel.Workbooks.Add()
With oBook.Worksheets(1)
.Columns("A:A").ColumnWidth = 13.88
.Columns("B:B").ColumnWidth = 5.75
.Columns("C:C").ColumnWidth = 7.5
.cells(4, 1) = Me.DataGrid1.Columns(0).Caption
.cells(4, 2) = Me.DataGrid1.Columns(1).Caption
.cells(4, 3) = Me.DataGrid1.Columns(2).Caption
For K = 0 To Me.DataGrid1.VisibleRows - 1
Me.DataGrid1.Row = K '这一步是关键
.cells(K + 5, 1) = Me.DataGrid1.Columns(0)
.cells(K + 5, 2) = Me.DataGrid1.Columns(1)
.cells(K + 5, 3) = Me.DataGrid1.Columns(2)
Next K
End With
oExcel.Visible = True
Set oBook = Nothing
Set oExcel = Nothing
end sub
追问
为什么你这个导出后Excel表格上面都要空出几行呢,不能把那几行删除吗?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询