xl( ) — Excel file I/O class 13
To read a range of data into Mata, you must specify the cell range by using a 1 × 2 rowvector. To
read row 1, columns B through F of a worksheet, code
string rowvector cells
real rowvector cols
cols = (2,6)
cells = b.get_string(1,cols)
To read rows 1 through 3 and columns B through D of a worksheet, code
real matrix cells
real rowvector rows, cols
rows = (1,3)
cols = (2,4)
cells = b.get_number(rows,cols)
Writing data to Excel
The following member functions of the xl() class are used to write data. row and col are real
scalars. When you write a matrix or vector, row and col are the starting (upper-left) cell in the
worksheet to which you want to begin saving.
B.put string(row, col, s)
writes a string scalar, vector, or matrix to an Excel worksheet.
B.put number(row, col, r
, "asdate" | "asdatetime" | "asdatenum" | asdatetimenum
)
writes a real scalar, vector, or matrix to an Excel worksheet.
B.put formula(row, col, s)
writes a string scalar, vector, or matrix containing valid Excel formulas to an Excel
worksheet.
B.put picture(row, col, filename)
writes a portable network graphics (.png), JPEG (.jpg), device-independent bitmap (.dib),
enhanced metafile (.emf), or tagged image file format (.tiff) file to an Excel worksheet.
B.put link(row, col, link, link text)
writes an HTTP link, Excel file link, or sheet link to an Excel worksheet.
To write the string "Auto Dataset" in cell A1 of a worksheet, code
b.put_string(1, 1, "Auto Dataset")
To write "mpg", "rep78", and "headroom" to cells B1 through D1 in a worksheet, code
names = ("mpg", "rep78", "headroom")
b.put_string(1, 2, names)
To write values 22, 17, 22, 20, and 15 to cells B2 through B6 in a worksheet, code
mpg_vals = (22\17\22\20\15)
b.put_number(2, 2, mpg_vals)
To sum the cells A1 through A4 in cell A6 in a worksheet, code
b.put_formula(1, 6, "SUM(A1:A4)")