使用AutoHotkey讀取Excel檔案的簡單說明

有網友詢問如何使用AutoHotkey讀取Excel檔,稍稍研讀了一下文件,簡述如本文。

  1. 使用ComObjCreate來建立Excel物件。
  2. ;;FileSelectFile, Path  ;; 可以用FileSelectFile開啟檔案總管來取要操作的檔案
      sPath := "e:\excel\test1.xls"
      oSheet := ComObjCreate("Excel.Application")
      oSheet.Workbooks.Open(sPath)  ; 開啟已存在的Excel檔案
      oSheet.Visible := True
    
  3. 用Excel物件.Range("A1").value將文字存入A1儲存格。
  4. oSheet.Range("A1").Value := "Excel測試!"  ; set cell 'A1' to a string
      sNewValue := "Excel測試!"
      oSheet.Range("A1").Value := sNewValue  ; set cell to a variable
    
  5. 將值寫入A行有內容的儲存格。A_Index是預設的迴圈計數器,由1遞增。
  6. sNewValue := "測試資料"
      while (oSheet.Range("A" . A_Index).Value != "") {
        oSheet.Range("A" . A_Index).Value := sNewValue
      }
    
  7. 將17個值寫入第6橫列。
  8. sRow := "6"
      Columns := Object(1,"A",2,"B",3,"C",4,"D",5,"E",6,"F",7,"G",8,"H",9,"I",10,"J",11,"K",12,"L",13,"M",14,"N",15,"O",16,"P",17,"Q") ;array of column letters
      For Key, Value In Columns
        oSheet.Range(Value . sRow).Value := sNewValue  ; set values of each cell in a row
    
  9. 依橫列數讀取有內容的A行到E行。
Loop, 5 {
  sColumn := Chr(A_Index+64)  ;; convert 1 to A, 2 to B, etc...
  ;; MsgBox %sColumn%
  while (oSheet.Range(sColumn . A_Index).Value != "") {
    cell := oSheet.Range(sColumn . A_Index).Value
    MsgBox (%sColumn%.%A_Index%)=%cell%
  }
}

參考

##

您可能也會有興趣的類似文章

簡睿

服務於軟體業的資訊老兵。興趣廣泛,學習力佳,樂於分享所知所學。

您可能也會喜歡…

2 個回應

  1. amnesiac表示:

    【进阶】使用 ADO 操作 Excel 文档
    http://zhuanlan.zhihu.com/autohotkey/19767178

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *