怎样把vb datagrid控件内容导入excel

来源:百度知道 编辑:UC知道 时间:2024/06/09 03:59:46
我做的datagrid控件动态显示access数据库里更新的信息,用的ADO,现在我想做个按纽控件,一点按纽就可以让datagrid里的内容全部导入到一个新建的excel文件。请教教我,最好能给出比较详细的代码和注释,我只学过VB基础,说的太简单我理解不了,我查了书,可是没有讲到这方面的内容,谢谢了。

Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet

Private Sub command6_Click()
Dim Irow, Icol As Integer
Dim Irowcount, Icolcount As Integer
Dim Fieldlen() As Integer
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets(1)
Adodc1.ConnectionString = "Provider=SQLOLEDB.1;Password=2752;Persist Security Info=True;User ID=sa;Initial Catalog=sample;Data Source=JAMLEEPC"
Adodc1.RecordSource = "select 编号,姓名,年龄 from " & s1 & " order by '编号'"
Adodc1.Refresh
With Adodc1.Recordset
Adodc1.Recordset.MoveLast
If .RecordCount < 1 Then
MsgBox ("Error 没有记录!")
Exit Sub
End If

Irowcount = .RecordCount '记录总数
Icolcount = .Fields