[ahk]读取excel文件实例

来源:互联网 发布:mac 桌面上 ds.store 编辑:程序博客网 时间:2024/05/20 06:54

EXCEL内容如下:

AutoHotkey代码如下:


#Persistent

#SingleInstance,force
;2015年1月4日
;sunwind
;读取excel实例
excel :=   ComObjActive("Excel.Application")
filepath:=A_ScriptDir . "\循环读取.xlsx"

;自动运行、初始化
Run notepad
xls:=Check(filepath,excel)  ;检测是否打开了xlsx文件
if (xls="") ;未打开则打开
{
    InputBox,_filepath,请打开相应Excel文件!,请输入xls路径,,300,200,,,,15,%filepath%
    if ErrorLevel
        ExitApp
    else
        RunWait %_filepath%
    ;持续等待,直到打开
    loop
    {
        xls:=Check(filepath,excel)
        ToolTip  等待excel启动完成
        Sleep,500
        if a_index>5
            MsgBox 等待太长时间了,请检查下。
    }until  IsObject(xls)
}
ToolTip
TrayTip ,,已就绪

;~ 若获取A2:B30区域值,实例如下:
r=30
arr := excel.Range["A2:B" r].value
;~ MsgBox % arr.MaxIndex(1)   ; total rows
;~ MsgBox % arr.MaxIndex(2)   ; total columns

; 创建图形界面
Gui, Add, ListView, r30 w180 gMyListView, 日期|报表名称
Loop, % arr.MaxIndex(1)
    {
        i:=A_Index
        LV_Add("", arr[i,1],arr[i,2])
    }
LV_ModifyCol()  ; 根据内容自动调整每列的大小.

Gui, Show
Gui,+AlwaysOnTop
return

MyListView:
if A_GuiEvent = DoubleClick
{
    LV_GetText(RowText, A_EventInfo,2)  ; 从行的第2个字段中获取文本.
    ToolTip You double-clicked row number %A_EventInfo%. Text: "%RowText%"
    ControlSend,Edit1,%RowText%,无标题
    ControlSend,Edit1,`n,无标题
}
return

GuiClose:  ; 表示当窗口关闭时脚本应自动退出.
ExitApp

;辅助函数
Check(filepath,oExcel)
{
    for Item in oExcel.workbooks
    {
        ;Name 文件名 FullName路径名
       ;~ data .=   "Name:`t"oexcel.workbooks(A_index).FullName  "`n"
       IF (oexcel.workbooks(A_index).FullName=filepath)
            xls:=item
    }
if IsObject(xls)
    return xls
else
    return ""
}
0 0
原创粉丝点击