首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

datagrid导出Excel的代码!解决方案

2012-01-23 
datagrid导出Excel的代码!各位,我想帮一个datagrid导出Excel的功能!应该怎么实现呢?有实例或者代码吗?谢谢

datagrid导出Excel的代码!
各位,我想帮一个datagrid导出Excel的功能!应该怎么实现呢?  
有实例或者代码吗?

谢谢了。520herenfeng@163.com    


[解决办法]
Option Explicit
Dim i, j, k As Integer
Dim strConn As String
Dim pubConn As New adodb.Connection
Dim rsTable As New adodb.Recordset
Dim strSQL As String
Dim xlapp As Variant
Dim xlBook As Variant
Dim xlSHEET As Variant

Private Sub Command1_Click()
Set xlapp = CreateObject( "excel.application ")
Set xlBook = xlapp.workbooks.Add
Set xlSHEET = xlBook.worksheets(1)
xlapp.Visible = True
On Error Resume Next
If Err.Number <> 0 Then Set xlapp = CreateObject( "Excel.Application ")
Set xlBook = xlapp.workbooks.Add
Set xlSHEET = xlBook.ActiveSheet
For k = 1 To DataGrid1.Columns.Count
xlSHEET.Cells(1, k) = DataGrid1.Columns(k - 1).Caption
Next k
For i = 1 To Adodc1.Recordset.RecordCount + 1
For j = 0 To DataGrid1.Columns.Count
xlSHEET.Cells(i + 1, j + 1) = Adodc1.Recordset(j) '
'xlSHEET.Cells(i + 1, j + 1) = DataGrid1.Columns(j).CellText(DataGrid1.RowBookmark(j))
Next j
Adodc1.Recordset.MoveNext
Next i
End Sub

Private Sub Form_Load()
strSQL = "select * from mdlk_sj where 批号= 'D012 ' "
Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & App.Path & "\hxrkgl.mdb;Persist Security Info=False "
Adodc1.RecordSource = strSQL
Adodc1.Refresh
End Sub

热点排行