libxl库能无需环境依赖实现后台读写xlsx表格,可用于AHK操控浏览器等采集数据后,用libxl库后台把数据整理保存下来。
以下展示libxl库能实现的效果,将下图正式表格,一键转换成下下图的原始数据表。
转换后的效果图
示例链接:
将数据全部读取到数组写入到新表.ahk
#Include %A_ScriptDir%/../Lib/libxl.ahk SetWorkingDir %A_ScriptDir% SetBatchLines -1 libxl.init(A_ScriptDir "/../Lib/64bit/libxl.dll") ; 初始化加载指定路径的libxl.dll /* book :=libxl.New("xls") ; 新建xls ; msgbox,% IsObject(book) book := libxl.Load("可读写的模板新.xls") ; 读取xls sheet :=book.getsheet(0) */ book := libxl.Load("测试读取模板.xlsx") sheet := book.getsheet(0) a := sheet.firstRow() b := sheet.lastRow() c := sheet.firstCol() d := sheet.lastCol() arr := [] Loop % b - a + 1 { x := A_Index Loop % d - c + 1 Try y := A_Index , arr.Push({ x : x, y : y, ct : sheet.readStr(x-1,y-1) }) ; catch ; arr.Push({x:x,y:y,ct:""}) ; 此步无效,会多出一行一列 } /* Loop,% arr.MaxIndex() { ;msgbox,% arr[A_Index].ct if RegExMatch(arr[A_Index].ct,"^\d{3}") drr.Push({x:arr[A_Index].x,y:arr[A_Index].y,ct:arr[A_Index].ct}) } */ drr := arr book2:=libxl.New("xlsx") sheet2 :=book2.Addsheet("转存内容") xx := 0 yy := -1 x0 := drr[1].x - 1 Loop % drr.MaxIndex() { x := drr[A_Index].x-1 y := drr[A_Index].y-1 if (x0=x) xx := xx, yy := yy + 1 else xx := xx + 1, yy := 0 Try sheet2.__Item[xx,yy] := drr[A_Index].ct x0 := x } book2.save("新表匹配数据.xlsx") book := book2 := "" ;dd :=sheet.readStr(0,5) ;dd2 :=sheet.readFormula(3,3) ;msgbox,% rowFirst "`n" rowLast "`n" colFirst "`n" colLast ;msgbox % "C4单元格值: " dd "`n表格(3,3)值: " dd2 "`n表格(3,3)值: " dd3 ;sheet.copyCell(5,1,13,3) ;msgbox,% sheet.writeStr(23, 2, "adasd") MsgBox 0x40040, , 新文档转存完毕 Return
声明:站内资源为整理优化好的代码上传分享与学习研究,如果是开源代码基本都会标明出处,方便大家扩展学习路径。请不要恶意搬运,破坏站长辛苦整理维护的劳动成果。本站为爱好者分享站点,所有内容不作为商业行为。如若本站上传内容侵犯了原著者的合法权益,请联系我们进行删除下架。
评论(0)