Adodc1.connectionstring = ......
Adodc1.RecordSource = "select * from 表1 where ......
Adodc1.Refresh
Set DataGrid1.DataSource = Adodc1
已经通过上面的语句把Access中"表1"的数据传到"DataGrid1"控件显示.
现在想把"DataGrid1"中选出的数据转到Excel文本中???请教各位大侠怎么实现啊?
Adodc1.RecordSource = "select * from 表1 where ......
Adodc1.Refresh
Set DataGrid1.DataSource = Adodc1
已经通过上面的语句把Access中"表1"的数据传到"DataGrid1"控件显示.
现在想把"DataGrid1"中选出的数据转到Excel文本中???请教各位大侠怎么实现啊?
On Error GoTo Errshow
Dim intHeadCnt As Integer
Dim CloneRS As New ADODB.Recordset
Set CloneRS = Excelrs.Clone
CloneRS.Filter = Excelrs.Filter
'Create a Recordset from all the records in the Orders table
Dim sNWind As String
'Dim Excelconn As New ADODB.Connection
'Dim Excelrs As ADODB.Recordset
'sNWind = "C:\Program Files\Microsoft Office\Office\Samples\Northwind.mdb"
'Excelconn.Open "Provider=sqloledb.1;user ID=sa;password=sa;data source=(local);initial catalog=bookshop"
'Excelconn.CursorLocation = adUseClient
'Set Excelrs = Excelconn.Execute("Orders", , adCmdTable)
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As ObjectSet oExcel = CreateObject("Excel.Application")
Set oBook = oExcel.Workbooks.Add
Set oSheet = oBook.Worksheets(1) For intHeadCnt = 0 To CloneRS.Fields.count - 1
oExcel.Worksheets(1).Cells(1, intHeadCnt + 1).value = CloneRS(intHeadCnt).Name
NextoSheet.Range("A2").CopyFromRecordset CloneRS
'oExcel.Visible = True
'oExcel.Sheets.PrintPreview
oBook.SaveAs FilePath
oExcel.Quit
Set oExcel = Nothing
Set oBook = Nothing
Set oSheet = Nothing
Call MsgBox("文件" & FilePath & "导出成功!", vbInformation, "提示")Exit Sub
'Excelrs.Close
'Excelconn.Close
Errshow:
MsgBox "不能创建xls文件,请确认已经安装Excel!"
End Sub数据源直接导
Set xlApp = CreateObject("Excel.Application") '创建EXCEL应用类
xlApp.Visible = True '设置EXCEL可见
Set xlBook = xlApp.Workbooks.Open("D:\终端销售跟踪统计系统\汇总表21.xlt") '打开EXCEL工作簿
Set xlsheet = xlBook.Worksheets(1) '打开EXCEL工作表
xlsheet.Activate '激活工作表
For i = 1 To DataGrid1.Columns.count
xlsheet.Cells(1, i) = DataGrid1.Columns(i - 1).Caption
For j = 0 To DataGrid1.VisibleRows - 1
xlsheet.Cells(j + 2, i) = DataGrid1.Columns(i - 1).CellText(DataGrid1.RowBook(j))
Next j
Next i
xlBook.RunAutoMacros (xlAutoOpen) '运行EXCEL中的启动宏
只能把DataGrid1框中当前页显示的数据导入到Excel,而未显示的数据(数据很多,右侧滑块下拉才能显示的数据)则不能导入到Excel中。估计和我用的循环界定的“DataGrid1.VisibleRows”有关!请问各位大侠如何改进,使得DataGrid1中所有的数据可以显示???
谢谢!!!