Introduction
46 | 47 |The module xlsx allows read and write access to .xlsx files.
48 | 49 | 50 | 51 |Example
52 | 53 | 54 |55 | require 'xlsx' 56 | 57 | local workbook = xlsx.Workbook(filename) 58 |59 | 60 | 61 | 62 | 63 |
64 |
xlsx Reference
65 | 66 |workbook = xlsx.Workbook([filename])
67 | 68 |Creates or opens a Workbook object. Returns a Workbook object.
69 | 70 |-
71 |
filename
is the name of the .xlsx file to open.
72 |
78 |
Workbook Reference
79 | 80 |totalWorksheets = #workbook
81 | 82 |Returns the total number of Excel worksheets in the current Excel workbook.
83 | 84 | 85 | 86 |worksheet = workbook[sheetIndex | name]
87 | 88 |Returns an object representing the Excel worksheet specified by sheetIndex
or name
. If no worksheet is found with the given sheetIndex
or name
, then nil
is returned.
-
91 |
sheetIndex
is an index in the range of 1 <=sheetIndex
<=GetTotalWorksheets
representing the sheet to retrieve.
92 | name
is a string representing the sheet name to retrieve.
93 |
workbook:Close()
98 | 99 |Closes the current Excel workbook.
100 | 101 | 102 | 103 | 104 |totalWorksheets = workbook:GetTotalWorksheets()
105 | 106 |Returns the total number of Excel worksheets in the current Excel workbook.
107 | 108 | 109 | 110 | 111 |worksheet = workbook:GetWorksheet(sheetIndex | name)
112 | 113 |Returns an object representing the Excel worksheet specified by sheetIndex
or name
. If no worksheet is found with the given sheetIndex
or name
, then GetWorksheet
returns nil
.
-
116 |
sheetIndex
is an index in the range of 0 <=sheetIndex
<GetTotalWorksheets()
representing the sheet to retrieve.
117 | name
is either a string or an xls.wchar representing the sheet name to retrieve.
118 |
sheetName = workbook:GetSheetName(sheetIndex)
124 | 125 |Returns either the name of the sheet.
126 | 127 |-
128 |
sheetIndex
is an index in the range of 1 <=sheetIndex
<=GetTotalWorksheets()
representing the worksheet to delete.
129 |
135 |
Worksheet Reference
136 | 137 |sheetName = worksheet:GetSheetName()
138 | 139 |Returns the name of the sheet.
140 | 141 | 142 | 143 | 144 |totalRows = worksheet:GetTotalRows()
145 | 146 |Returns the total number of rows in the Excel worksheet.
147 | 148 | 149 | 150 | 151 |totalColumns = worksheet:GetTotalCols()
152 | 153 |Returns the total number of columns in the Excel worksheet.
154 | 155 | 156 | 157 |cell = worksheet:Cell(row, col)
158 | 159 |Retrieves the contents of a cell from the Excel worksheet.
160 | 161 |Returns the cell if the operation succeeded, nil
if either the row or column are not in range.
-
164 |
row
is a value from 0 to 65535, representing the row in the Excel worksheet to retrieve.
165 | col
is a value from 0 to 255, representing the column in the Excel worksheet to retrieve.
166 |
cell = worksheet.COLROW
172 | 173 |Retrieves the contents of a cell from the Excel worksheet.
174 | 175 |Returns the cell if the operation succeeded, nil
if either the row or column are not in range.
-
178 |
COLROW
is a column and row in Excel format, such as A4 or BD12.
179 |
186 |
Cell Reference
187 | 188 |cellType = cell:Type()
189 | 190 |Returns one of the following as the type of this Excel cell.
191 | 192 |-
193 |
cell.UNDEFINED
194 | cell.BOOLEAN
195 | cell.INT
196 | cell.DOUBLE
197 | cell.STRING
198 | cell.WSTRING
199 | cell.FORMULA
200 |
cellContent = cell.value
205 | 206 |Returns the raw value of the cell.
207 | 208 | 209 | 210 | 211 |cellContent = cell:Get()
212 | 213 |If the type of the cell is cell.BOOLEAN
, the boolean content of the cell is returned as a Lua boolean. If the type of the cell is cell.INT
or cell.DOUBLE
, the integer or double content of the cell is returned as a Lua number. If the type of the cell is cell.STRING
, the ANSI string content of the cell is returned as a Lua string. If the type of the cell is cell.WSTRING
, the Unicode string content of the cell are returned as an xls.wchar
. Otherwise, nil
is returned.
cellContent = cell:GetBoolean()
218 | 219 |If the type of the cell is cell.BOOLEAN
, the boolean content of the cell is returned as a Lua boolean. Otherwise, nil
is returned.
cellContent = cell:GetInteger()
224 | 225 |If the type of the cell is cell.INT
, the integer content of the cell is returned as a Lua number. Otherwise, nil
is returned.
cellContent = cell:GetDouble()
230 | 231 |If the type of the cell is cell.DOUBLE
, the double content of the cell is returned as a Lua number. Otherwise, nil
is returned.
cellContent = cell:GetString()
236 | 237 |If the type of the cell is cell.STRING
, the ANSI string content of the cell is returned as a Lua string. Otherwise, nil
is returned.