├── Example.ahk ├── ExcelToArray.ahk ├── README.md └── test.xlsx /Example.ahk: -------------------------------------------------------------------------------- 1 | #NoEnv 2 | #SingleInstance Force 3 | SetBatchLines -1 4 | #Include ExcelToArray.ahk 5 | 6 | Gui, Add, ListView, xm w700 r10 Grid, A|B|C|D|E|F|G|H|I 7 | Loop, 9 8 | LV_ModifyCol(A_Index, 75) 9 | Gui, Show,, Get Listview data from excel 10 | 11 | SplashTextOn, , , Loading... 12 | Gosub, ImportData 13 | SplashTextOff 14 | Return 15 | 16 | ImportData: 17 | arr := ExcelToArray("test.xlsx") 18 | 19 | for i, dat in arr 20 | LV_Add("", dat*) 21 | Return 22 | 23 | GuiClose: 24 | ExitApp -------------------------------------------------------------------------------- /ExcelToArray.ahk: -------------------------------------------------------------------------------- 1 | ; v1.1 (2018-7-19) 2 | ; https://github.com/tmplinshi/ExcelToArray 3 | 4 | ExcelToArray(FileName, nSheet := 1, last_row := "", last_column := "") 5 | { 6 | return ExcelToArray.DoIt(FileName, nSheet, last_row, last_column) 7 | } 8 | 9 | class ExcelToArray 10 | { 11 | DoIt(FileName, nSheet := 1, last_row := "", last_column := "") 12 | { 13 | if !FileExist(FileName) 14 | throw, "File Not Exist!" 15 | 16 | safeArr := this.GetSafeArrFromXlFile(FileName, nSheet, last_row, last_column) 17 | ret := this.SafeArr_To_AHKArr(safeArr) 18 | return ret 19 | } 20 | 21 | GetSafeArrFromXlFile(FileName, nSheet := 1, last_row := "", last_column := "") 22 | { 23 | fPath := this.GetFullPath(FileName) 24 | 25 | if this.IsFileInUse(fPath) { 26 | try wb := this.GetWorkbook(fpath) 27 | } 28 | if !wb { 29 | xlObj := ComObjCreate("Excel.Application") 30 | xlObj.Workbooks.Open(fPath) 31 | wb := xlObj.ActiveWorkbook 32 | } 33 | 34 | safeArr := this.GetSafeArr(wb, nSheet, last_row, last_column) 35 | 36 | xlObj.Quit 37 | 38 | return safeArr 39 | } 40 | 41 | GetWorkbook(fPath) 42 | { 43 | xls := ComObjActive("Excel.Application") 44 | 45 | Loop, % xls.WorkBooks.Count 46 | { 47 | if ( xls.WorkBooks(A_Index).FullName = fPath ) 48 | return xls.WorkBooks(A_Index) 49 | } 50 | } 51 | 52 | SafeArr_To_AHKArr(SafeArr) 53 | { 54 | ret := [] 55 | 56 | rowCount := SafeArr.MaxIndex(1) 57 | colCount := SafeArr.MaxIndex(2) 58 | 59 | Loop, % rowCount 60 | { 61 | row := A_Index 62 | 63 | arr := [] 64 | Loop, % colCount 65 | arr.push( SafeArr[row, A_Index] ) 66 | 67 | ret.push(arr) 68 | } 69 | 70 | return ret 71 | } 72 | 73 | GetSafeArr(oWorkbook, nSheet := 1, last_row := "", last_column := "") 74 | { 75 | sheet := oWorkbook.Sheets(nSheet) 76 | 77 | if last_row && last_column 78 | lastCell := {row: last_row, column: last_column} 79 | else 80 | { 81 | lastCell := this.xlFindLastCell(oWorkbook, nSheet) 82 | if last_row 83 | lastCell.row := last_row 84 | else if last_column 85 | lastCell.column := last_column 86 | } 87 | cell_begin := sheet.cells(1, 1) 88 | cell_end := sheet.cells(lastCell.row, lastCell.column) 89 | 90 | return safeArr := sheet.Range(cell_begin, cell_end).FormulaR1C1 91 | } 92 | 93 | GetFullPath(FileName) 94 | { 95 | Loop, % FileName 96 | return A_LoopFileLongPath 97 | } 98 | 99 | IsFileInUse(FileName) 100 | { 101 | return FileExist(FileName) && !FileOpen(FileName, "rw") 102 | } 103 | 104 | xlFindLastCell(oWorkbook, sheet := 1) 105 | { 106 | static xlByRows := 1 107 | , xlByColumns := 2 108 | , xlPrevious := 2 109 | 110 | lastRow := oWorkbook.Sheets(sheet).Cells.Find("*", , , , xlByRows , xlPrevious).Row 111 | lastCol := oWorkbook.Sheets(sheet).Cells.Find("*", , , , xlByColumns, xlPrevious).Column 112 | 113 | return {row: lastRow, column: lastCol} 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ExcelToArray 2 | Read excel data to AHK array 3 | 4 | ### Usage 5 | `arr := ExcelToArray(FileName, nSheet, last_row, last_column)` 6 | - `FileName` - The excel file path. 7 | - `nSheet` - (Optional) Sheet number. Default is 1. 8 | - `last_row` - (Optional) Last row number. 9 | - `last_column` - (Optional) Last column number. 10 | 11 | Example of output array: 12 | ```Json 13 | [ 14 | ["a1", "b1", "c1"], 15 | ["a2", "b2", "c2"] 16 | ] 17 | ``` 18 | 19 | Note: This function uses `sheet.Range(cell_begin, cell_end).FormulaR1C1` (instead of `.Value`) to read the data, it can avoids the numbers such as `1.2` converted into `1.200000`. But if the cell contains formula, the formula code itself will be read, not the actual value. 20 | 21 | **Related Function** 22 | - [Create excel file from array or listview](https://gist.github.com/tmplinshi/7e2d75794e58def0d43e) 23 | -------------------------------------------------------------------------------- /test.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tmplinshi/ExcelToArray/c71880012b5129e42e4712695e2f5f88689b0950/test.xlsx --------------------------------------------------------------------------------