Dim tempxlApp As New Excel.Application Dim tempxlWorkbook As New Excel.Workbook Dim tempxlSheet As New Excel.Worksheet Dim cmSe As ADODB.Command Dim strXlsName As String 'Excel 文件名 Dim strSheet As String '表名 Dim strPcode As String '产品编号 Dim strTmp As String Dim i As Integer Set cmSe = New ADODB.Command cmSe.ActiveConnection = cn cmSe.CommandType = adCmdText strXlsName= app.path & "\Book1.xls" '打开Excel 文件 Set tempxlWorkbook = tempxlApp.Workbooks.Open(strXlsName) tempxlApp.DisplayAlerts = False Set tempxlSheet = tempxlWorkbook.Worksheets("Sheet1") tempxlSheet.Select
For i = 1 To 50 strPcode = tempxlSheet.Cells(i, 5) iNum = tempxlSheet.Cells(i, 17) '把记录添加到EachDay表中 strTmp = "Insert into EachDay (cInvCode,iCount) VALUES('" & strPcode & "'," & iNum & ")" cmSe.CommandText = strTmp cmSe.Execute Next i
'释放对象,关闭excel Set tempxlSheet = Nothing Set tempxlWorkbook = Nothing tempxlApp.Quit Set tempxlApp = Nothing
数据库的内容写到Excel中不难,特定的Excel是指定的Excel文件吗?你先打开这个文件,让它在后台运行,这样就不显示了。然后从数据库中读取数据再写向Excel。具体的细节,可以在论坛中用Excel关键字查询。
Dim tempxlWorkbook As New Excel.Workbook
Dim tempxlSheet As New Excel.Worksheet
Dim cmSe As ADODB.Command Dim strXlsName As String 'Excel 文件名
Dim strSheet As String '表名
Dim strPcode As String '产品编号
Dim strTmp As String
Dim i As Integer Set cmSe = New ADODB.Command
cmSe.ActiveConnection = cn
cmSe.CommandType = adCmdText strXlsName= app.path & "\Book1.xls"
'打开Excel 文件
Set tempxlWorkbook = tempxlApp.Workbooks.Open(strXlsName)
tempxlApp.DisplayAlerts = False
Set tempxlSheet = tempxlWorkbook.Worksheets("Sheet1")
tempxlSheet.Select
For i = 1 To 50
strPcode = tempxlSheet.Cells(i, 5)
iNum = tempxlSheet.Cells(i, 17)
'把记录添加到EachDay表中
strTmp = "Insert into EachDay (cInvCode,iCount) VALUES('" & strPcode & "'," & iNum & ")"
cmSe.CommandText = strTmp
cmSe.Execute
Next i
'释放对象,关闭excel
Set tempxlSheet = Nothing
Set tempxlWorkbook = Nothing
tempxlApp.Quit
Set tempxlApp = Nothing