VB中如何将DataGrid的数据输出到Excel中?
在VB中,通过SQL语言查询的数据放在DataGrid中显示,希望把输出DataGrid显示的数据输出到Excel中去?怎么写代码啊?注:有vb实例程序的加30分,谢谢!...
在VB中,通过SQL语言查询的数据放在DataGrid中显示,希望把输出DataGrid显示的数据输出到Excel中去?怎么写代码啊?
注:有vb实例程序的加30分,谢谢!
是在VB6中用SQL语言查询Access2000数据库 ,邮箱:yjliwhu@foxmail.com 展开
注:有vb实例程序的加30分,谢谢!
是在VB6中用SQL语言查询Access2000数据库 ,邮箱:yjliwhu@foxmail.com 展开
3个回答
展开全部
这个代码是vb.net 的
vb 6 里你吧 Microsoft.Office.Interop.Excel 的com引用进来
也许不用改 也可用
就这个模式 ,
Dim excel As New Microsoft.Office.Interop.Excel.ApplicationClass
Dim wBook As Microsoft.Office.Interop.Excel.Workbook
Dim wSheet As Microsoft.Office.Interop.Excel.Worksheet
wBook = excel.Workbooks.Add()
wSheet = wBook.ActiveSheet()
Dim dt As System.Data.DataTable = dset.Tables(0)
Dim dc As System.Data.DataColumn
Dim dr As System.Data.DataRow
Dim colIndex As Integer = 0
Dim rowIndex As Integer = 0
For Each dc In dt.Columns
colIndex = colIndex + 1
excel.Cells(1, colIndex) = dc.ColumnName
Next
For Each dr In dt.Rows
rowIndex = rowIndex + 1
colIndex = 0
For Each dc In dt.Columns
colIndex = colIndex + 1
excel.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)
Next
Next
wSheet.Columns.AutoFit()
Dim strFileName As String = "D:\ss.xls"
Dim blnFileOpen As Boolean = False
Try
Dim fileTemp As System.IO.FileStream = System.IO.File.OpenWrite(strFileName)
fileTemp.Close()
Catch ex As Exception
blnFileOpen = False
End Try
If System.IO.File.Exists(strFileName) Then
System.IO.File.Delete(strFileName)
End If
wBook.SaveAs(strFileName)
excel.Workbooks.Open(strFileName)
excel.Visible = True
vb 6 里你吧 Microsoft.Office.Interop.Excel 的com引用进来
也许不用改 也可用
就这个模式 ,
Dim excel As New Microsoft.Office.Interop.Excel.ApplicationClass
Dim wBook As Microsoft.Office.Interop.Excel.Workbook
Dim wSheet As Microsoft.Office.Interop.Excel.Worksheet
wBook = excel.Workbooks.Add()
wSheet = wBook.ActiveSheet()
Dim dt As System.Data.DataTable = dset.Tables(0)
Dim dc As System.Data.DataColumn
Dim dr As System.Data.DataRow
Dim colIndex As Integer = 0
Dim rowIndex As Integer = 0
For Each dc In dt.Columns
colIndex = colIndex + 1
excel.Cells(1, colIndex) = dc.ColumnName
Next
For Each dr In dt.Rows
rowIndex = rowIndex + 1
colIndex = 0
For Each dc In dt.Columns
colIndex = colIndex + 1
excel.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)
Next
Next
wSheet.Columns.AutoFit()
Dim strFileName As String = "D:\ss.xls"
Dim blnFileOpen As Boolean = False
Try
Dim fileTemp As System.IO.FileStream = System.IO.File.OpenWrite(strFileName)
fileTemp.Close()
Catch ex As Exception
blnFileOpen = False
End Try
If System.IO.File.Exists(strFileName) Then
System.IO.File.Delete(strFileName)
End If
wBook.SaveAs(strFileName)
excel.Workbooks.Open(strFileName)
excel.Visible = True
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你这种方式只能输出DATAGRID当前显示的那一页,
如果你禁用DATAGRID的分页的显示,便可以输出
整个查询结果集了。
祝你好运!
请参考
如果你禁用DATAGRID的分页的显示,便可以输出
整个查询结果集了。
祝你好运!
请参考
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
太复杂了,简单搞定:
On Error Resume Next
MousePointer = 11
CMG.Filename = ""
CMG.InitDir = App.Path + "\xls\"
CMG.Filter = "EXCEL文件(*.xls)|*.xls"
CMG.ShowSave
If CMG.Filename <> "" Then
DB.Execute "SELECT * INTO [Excel 8.0;DATABASE=" & CMG.Filename & "].[导出表] FROM [" & Bm & "]" & SQLWhere & SQLOrder
Else
MsgBox "操作被取消!"
MousePointer = 1
Exit Sub
End If
MousePointer = 1
MsgBox "已成功导出!"
On Error Resume Next
MousePointer = 11
CMG.Filename = ""
CMG.InitDir = App.Path + "\xls\"
CMG.Filter = "EXCEL文件(*.xls)|*.xls"
CMG.ShowSave
If CMG.Filename <> "" Then
DB.Execute "SELECT * INTO [Excel 8.0;DATABASE=" & CMG.Filename & "].[导出表] FROM [" & Bm & "]" & SQLWhere & SQLOrder
Else
MsgBox "操作被取消!"
MousePointer = 1
Exit Sub
End If
MousePointer = 1
MsgBox "已成功导出!"
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询