EXCEL操作读取出错[已解决]
本帖最后由 仙乃日 于 2020-9-11 23:01 编辑#include <Array.au3>
#include <File.au3>
#include <_Ini.au3>
#include <Date.au3>
If ProcessExists("Excel.exe") Then
Do
Sleep(200)
ProcessClose("Excel.exe")
Until Not ProcessExists("Excel.exe")
EndIf
Local $Excel_dir = @ScriptDir & '\每日汇总名单\'
If DirGetSize($Excel_dir) = -1 Then DirCreate($Excel_dir)
Local $ini_file2 = @ScriptDir & '\每日汇总名单\进度.ini'
If Not FileExists($ini_file2) Then
FileClose(FileOpen($ini_file2, 9))
EndIf
Local $FoldersList = _FileListToArray(@ScriptDir & "\583001", "*", 2);;返回目录
If Not @error Then
Local $o_Excel = ObjCreate("excel.application")
$o_Excel.visible = 1 ;是否显示界面
$o_Excel.DisplayAlerts = False;不弹出警告
;$o_Excel.Application.ScreenUpdating = False ;取消屏幕更新
For $F = 1 To $FoldersList
Local $read_jd = FileRead($ini_file2)
Local $ini_file = @ScriptDir & "\583001\" & $FoldersList[$F] & "\583001条码信息.ini"
;Global $Spath = StringLeft(@ScriptDir, StringInStr(@ScriptDir, '\', 0, -1 - StringInStr(StringRight(@ScriptDir, 1), '\')) - 1);;上级路径 不带反'\'
If Not FileExists($ini_file) Then
MsgBox(64, "警告", $ini_file & "不存在或者损坏!")
Exit
EndIf
If StringInStr($read_jd, $ini_file) = 0 Then
Local $secName = _IniReadSectionNames($ini_file)
Local $row = 0
;---------Excel操作开始-----------------------------------------------------
$o_Workbook = $o_Excel.workbooks.add
$o_Workbook.Application.ReferenceStyle = -4150 ;设置为数字引用样式
$o_Workbook.activesheet.cells.numberformatlocal = "@";设置为文本
;---------Excel操作结束-----------------------------------------------------
For $i = 1 To $secName
Local $str1_barcode = $secName[$i] ;条码
$row = $i + 1
If $o_Workbook.activesheet.Cells(1, 1).Value = "" Then;------先填写第1行的标题
$o_Workbook.activesheet.Cells(1, 1).Value = "身份证号"
$o_Workbook.activesheet.Cells(1, 2).Value = "姓名"
$o_Workbook.activesheet.Cells(1, 3).Value = "性别"
$o_Workbook.activesheet.Cells(1, 4).Value = "年龄"
$o_Workbook.activesheet.Cells(1, 5).Value = "联系电话"
$o_Workbook.activesheet.Cells(1, 6).Value = "村别"
$o_Workbook.activesheet.Cells(1, 7).Value = "序号"
$o_Workbook.activesheet.Cells(1, 8).Value = "原始条码"
$o_Workbook.activesheet.Cells(1, 9).Value = "登记时间"
$o_Workbook.activesheet.Cells(1, 10).Value = "备注"
#cs
Local $Ini_Section = IniReadSection($ini_file, $secName);读取第1个人的所有键值
For $k = 1 To $Ini_Section
Local $Ini_Section_Key = $Ini_Section[$k];;键名
$o_Workbook.activesheet.Cells(1, $k + 1).Value = $Ini_Section_Key
Next
#ce
EndIf
Local $idcardno = IniRead($ini_file, $str1_barcode, "身份证号", "")
Local $bq_number = IniRead($ini_file, $str1_barcode, "序号", "")
Local $rq8 = StringMid($bq_number, 1, 8)
Local $rq = IniRead($ini_file, $str1_barcode, "时间", "")
Local $name = IniRead($ini_file, $str1_barcode, "姓名", "")
Local $Tel = IniRead($ini_file, $str1_barcode, "电话", "")
Local $Address = IniRead($ini_file, $str1_barcode, "地址", "")
Local $Born = StringRegExpReplace($idcardno, '\d{6}(\d{4})(\d{2})(\d{2})\d{3,4}+', '$1/$2/$3')
Local $age = _DateDiff('Y', $Born, _NowCalc());年龄
Local $Sex = ""
Local $x = Mod(StringMid($idcardno, 17, 1), 2)
If $x = 1 Then $Sex = "男"
If $x = 0 Then $Sex = "女"
$o_Workbook.activesheet.Cells($row, 1).Value = $idcardno
$o_Workbook.activesheet.Cells($row, 2).Value = $name
$o_Workbook.activesheet.Cells($row, 3).Value = $Sex
$o_Workbook.activesheet.Cells($row, 4).Value = $age
$o_Workbook.activesheet.Cells($row, 5).Value = $Tel
$o_Workbook.activesheet.Cells($row, 6).Value = cb_Address($Address)
$o_Workbook.activesheet.Cells($row, 7).Value = $bq_number
$o_Workbook.activesheet.Cells($row, 8).Value = $str1_barcode
$o_Workbook.activesheet.Cells($row, 9).Value = $rq
Local $ybzk_sg = IniRead($ini_file, $str1_barcode, "ybzk_sg", "");身高
Local $ybzk_zssy = IniRead($ini_file, $str1_barcode, "ybzk_zssy", "");
If Not (Number($ybzk_sg) > 100 Or Number($ybzk_zssy) > 10) Then
$o_Workbook.activesheet.Cells($row, 10).Value = "未检"
EndIf
Next
$o_Excel.Cells.EntireColumn.AutoFit;所有列自动列宽
$o_Workbook.saveas($Excel_dir & $FoldersList[$F] & "每日汇总名单.xlsx", 51);xls是56;xlsx是51;csv是6;
$o_Workbook.close()
IniWrite($ini_file2, "汇总进度", $ini_file, $FoldersList[$F])
EndIf
Next
$o_Excel.Quit()
EndIf
一般执行循环第3-4次时,就会出现如图的内存不能读取的错误。 单个操作时则完全正常,实在想不出是什么原因??
最后测试发现是AU3版本的问题,之前使用的是EXCEL2016 与AU3经典3.3.13.12版本。更换AU3到发哥推荐的Autoit3.3.15.3就不再出现内存不能读取的错误了。
Autoit3.3.15.3 不再支持#AutoIt3Wrapper_UseAnsi=y
页:
[1]