[已解決]MDB 匯出 指定表數據 到 xls
本帖最后由 w60711 于 2017-12-26 19:12 编辑请教一下
我从MDB读取数据,要汇出到xls
以下为代码
但是出现一个问题
只汇出了一笔数据,且是MDB最后一笔
请教如何修改才能汇出MDB指定表全部的数据
感谢
Func SaveWorkerTable();;汇出资料
$MyDocsFolder = "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"
$FileSavePath = FileSaveDialog("选择储存位置", $MyDocsFolder, "Excel档 (*.xls)|所有档 (*.*)" , 2 + 16 , "人员资料_"&@YEAR&@MON&@MDAY&'-'&@HOUR&@MIN&@SEC)
$addfld = ObjCreate("ADODB.Connection")
$addfld.Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & $DBName & ";Jet Oledb:Database Password="&$mdb_data_pwd)
$RS =ObjCreate("ADODB.Recordset")
$RS.ActiveConnection = $addfld
$RS.Open ("Select * From " & $DBEnterpriseTable )
Local $oExcel = _ExcelBookNew(0);;Create new book, make it visible
; Write something into cell
_ExcelWriteCell($oExcel,'索引号',1,1)
_ExcelWriteCell($oExcel,'统一编号',1,2)
_ExcelWriteCell($oExcel,'厂商名称',1,3)
_ExcelWriteCell($oExcel,'人员姓名',1,4)
_ExcelWriteCell($oExcel,'市话',1,5)
_ExcelWriteCell($oExcel,'传真',1,6)
_ExcelWriteCell($oExcel,'手机',1,7)
_ExcelWriteCell($oExcel,'地址',1,8)
_ExcelWriteCell($oExcel,'备注事项',1,9)
while Not $RS.eof And Not $RS.bof
if @error =1 Then ExitLoop
$i = 1
$n = $i + 1
_ExcelWriteCell($oExcel,$RS.Fields(0).value,$n,1)
_ExcelWriteCell($oExcel,$RS.Fields(1).value,$n,2)
_ExcelWriteCell($oExcel,$RS.Fields(2).value,$n,3)
_ExcelWriteCell($oExcel,$RS.Fields(3).value,$n,4)
_ExcelWriteCell($oExcel,$RS.Fields (4).value&"-"& $RS.Fields (5).value&"-"& $RS.Fields (6).value&"#"& $RS.Fields (7).value,$n,5)
_ExcelWriteCell($oExcel,$RS.Fields (8).value&"-"& $RS.Fields (9).value&"-"& $RS.Fields (10).value,$n,6)
_ExcelWriteCell($oExcel,$RS.Fields (11).value&"-"& $RS.Fields (12).value&"-"& $RS.Fields (13).value&"#"& $RS.Fields (7).value,$n,7)
_ExcelWriteCell($oExcel,$RS.Fields(14).value,$n,8)
_ExcelWriteCell($oExcel,$RS.Fields(15).value,$n,9)
$rs.movenext
WEnd
$rs.close
$addfld.Close
_ExcelBookSaveAs($oExcel,$FileSavePath,'xls',0,1)
_ExcelBookClose($oExcel)
MsgBox (64,"完成","已成功储存档案!" & @CRLF & @CRLF & "档案路径为:" & $FileSavePath)
Return
EndFunc 回复 1# w60711
AutoIt3\autoit3.exe用的是什么版本? 本帖最后由 chzj589 于 2017-12-20 13:56 编辑
回复 1# w60711
这样试试:
Func SaveWorkerTable();;汇出资料
$MyDocsFolder = "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"
$FileSavePath = FileSaveDialog("选择储存位置", $MyDocsFolder, "Excel档 (*.xls)|所有档 (*.*)" , 2 + 16 , "人员资料_"&@YEAR&@MON&@MDAY&'-'&@HOUR&@MIN&@SEC)
$addfld = ObjCreate("ADODB.Connection")
$addfld.Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & $DBName & ";Jet Oledb:Database Password="&$mdb_data_pwd)
$RS =ObjCreate("ADODB.Recordset")
$RS.ActiveConnection = $addfld
$RS.Open ("Select * From " & $DBEnterpriseTable )
Local $oExcel = _ExcelBookNew(1);;Create new book, make it visible
$L = "|||||报表|||||"
$L1 = '索引号|统一编号|厂商名称|人员姓名|市话|传真|手机|地址|备注事项'
Local $htesta = StringSplit($L, '|')
Local $htest = StringSplit($L1, '|')
$oExcel.activesheet.cells(1, 5).value = $htesta;.name
For $id = 0 To $htest
$oExcel.activesheet.cells(2, $id).Value = $htest[$id]
Next
$oExcel.activesheet.cells(3, 1).CopyFromRecordset($RS)
$RS.close
$addfld.Close
MsgBox (64,"完成","已成功储存档案!" & @CRLF & @CRLF & "档案路径为:" & $FileSavePath)
Return
EndFunc
循环有问题。把$i=1 拿到循环前面去。你是始终往同一行写,覆盖掉了 修正後可以用的源碼
Func SaveWorkerTable();;汇出资料
$MyDocsFolder = "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"
$FileSavePath = FileSaveDialog("选择储存位置", $MyDocsFolder, "Excel档 (*.xls)|所有档 (*.*)" , 2 + 16 , "人员资料_"&@YEAR&@MON&@MDAY&'-'&@HOUR&@MIN&@SEC)
$addfld = ObjCreate("ADODB.Connection")
$addfld.Open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & $DBName & ";Jet Oledb:Database Password="&$mdb_data_pwd)
$RS =ObjCreate("ADODB.Recordset")
$RS.ActiveConnection = $addfld
$RS.Open ("Select * From " & $DBEnterpriseTable )
Local $oExcel = _ExcelBookNew(0);;Create new book, make it visible
; Write something into cell
_ExcelWriteCell($oExcel,'索引号',1,1)
_ExcelWriteCell($oExcel,'统一编号',1,2)
_ExcelWriteCell($oExcel,'厂商名称',1,3)
_ExcelWriteCell($oExcel,'人员姓名',1,4)
_ExcelWriteCell($oExcel,'市话',1,5)
_ExcelWriteCell($oExcel,'传真',1,6)
_ExcelWriteCell($oExcel,'手机',1,7)
_ExcelWriteCell($oExcel,'地址',1,8)
_ExcelWriteCell($oExcel,'备注事项',1,9)
Dim $i = 1
while Not $RS.eof And Not $RS.bof
$i = $i + 1
if @error =1 Then ExitLoop
_ExcelWriteCell($oExcel,$RS.Fields(0).value,$i,1)
_ExcelWriteCell($oExcel,$RS.Fields(1).value,$i,2)
_ExcelWriteCell($oExcel,$RS.Fields(2).value,$i,3)
_ExcelWriteCell($oExcel,$RS.Fields(3).value,$i,4)
_ExcelWriteCell($oExcel,$RS.Fields(4).value&"-"& $RS.Fields(5).value&"-"& $RS.Fields(6).value&"#"& $RS.Fields(7).value,$i,5)
_ExcelWriteCell($oExcel,$RS.Fields(8).value&"-"& $RS.Fields(9).value&"-"& $RS.Fields(10).value,$i,6)
_ExcelWriteCell($oExcel,$RS.Fields(11).value&"-"& $RS.Fields(12).value&"-"& $RS.Fields(13).value&"#"& $RS.Fields(7).value,$i,7)
_ExcelWriteCell($oExcel,$RS.Fields(14).value,$i,8)
_ExcelWriteCell($oExcel,$RS.Fields(15).value,$i,9)
$rs.movenext
WEnd
$rs.close
$addfld.Close
_ExcelBookSaveAs($oExcel,$FileSavePath,'xls',0,1)
_ExcelBookClose($oExcel)
MsgBox (64,"完成","已成功储存档案!" & @CRLF & @CRLF & "档案路径为:" & $FileSavePath)
Return
EndFunc
回复 5# w60711
导出的速度如何呢?
我对导出要改变数据库数据的处理方法是:
从ListView列表导出。在3.3.14.2版本上导出速度也很快。 本帖最后由 w60711 于 2018-1-1 14:27 编辑
回复 6# chzj589
我是3.3.14.2
我那个方法
数据大的话效率不佳...
汇出5000多笔而已就花了很长时间
您的方法很快
但有办法汇出后如下面图片一样格式吗?
感谢您
用chzj589 的方法。使用excel的CopyFromRecordset方法速度应当快 本帖最后由 chzj589 于 2018-1-1 20:50 编辑
回复 7# w60711
回答是恳定的,只要你将数据库读取显示在ListView是什么样式,导出就是什么样式。
查看短信
页:
[1]