本帖最后由 zhaoceshi 于 2021-6-5 21:46 编辑
有2个问题想请教下各位大神们。
一是,在使用Excel函数去读取Excel表里的数据,比如sheet1表的B列,然后把数据写入新建文本里面,发现效率很慢。看看大神有没有高效的操作方法?
二是,在使用$oWorkbook.Worksheets("sheet4").Usedrange.Columns 函数时,如果Excel表格里没有"sheet4"表,脚本就会直接报错退出运行。有没有方法可以判断没有指定的表格就直接跳过?
=======6.1
今天发现,Excel函数是不是有问题?只用用for循环函数循环多几次(3-6次)就会报变量错误,如果是手动运行多少次都不会。真是奇怪。使用64位编辑就不会报错
大致代码如下:#include <Array.au3>
#include <Excel.au3>
#include <MsgBoxConstants.au3>
;~ opt("MustDeclareVars", 1)
Local $7z = "c:\222\7z.exe", $date = "c:\统计", $sfile, $sname, $a = 1
$sfile = FileFindFirstFile($date & "*.zip")
While 1
$sname = FileFindNextFile($sfile)
If @error Then ExitLoop
$a = $a
RunWait(@ComSpec & " /c " & $7z & " x " & $date & $sname & " -o" & $date & "\解压后" & $a & "-" & StringReplace($sname, ".zip", "") & " -aoa", "", @SW_HIDE) ;
FileWriteLine($date & "\run.log",$a & "-" & StringReplace($sname, ".zip", ""))
Local $oExcel = _Excel_Open() ;(True,True) 为提示,否则
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "错误", "创建 Excel 应用对象发生错误." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
Local $oWorkbook = _Excel_BookOpen($oExcel, $date & "\解压后" & $a & "-" & StringReplace($sname, ".zip", "")&"\index.xls",True,False)
If @error Then
MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeRead 示例", "打开工作簿发生错误 '" & $date & "\解压后" & $a & "-" & StringReplace($sname, ".zip", "")&"\index.xls" & @CRLF & "@error = " & @error & ", @extended = " & @extended)
_Excel_Close($oExcel)
Exit
EndIf
Local $ip = _Excel_RangeRead($oWorkbook,Default, $oWorkbook.Worksheets("sheet1").Usedrange.Columns("b:b")) ;$oWorkbook.ActiveSheet.Usedrange.Columns("b:b"))
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeRead 示例 3", "读工作簿失败." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
For $i = 0 To UBound($ip) - 1
If $ip[$i] = "" Or $ip[$i] = "错误" Then ContinueLoop
FileWriteLine($date&"关注信息.txt", $ip[$i])
Next
Local $ld = _Excel_RangeRead($oWorkbook,Default, $oWorkbook.Worksheets("sheet4").Usedrange.Columns("d:K"))
If @error Then ContinueLoop
For $i = 0 To UBound($ld) - 1
If $ld[$i][7] = "影响" Or $ld[$i][7] = "" Then ContinueLoop
FileWriteLine($date&$ld[$i][0]&".txt",$ld[$i][7])
Next
$a = $a + 1
_Excel_BookClose($oWorkbook,False)
_Excel_Close($oExcel)
WEnd
|