xlswrite

Writes data to file, which is either an Excel-compatible file or a structure returned from xlsopen. This command is only available on Windows at this time.

Syntax

R = xlswrite(file, data,...)

R = xlswrite(file, data, w,...)

R = xlswrite(file, data, w, range,...)

R = xlswrite(file, data, w, range, 'interface', 'itype')

Inputs

file
Name of the file to write or a structure returned as the result of an xlsopen. If file is a filename, the file extension must be Excel-compatible. If file is a structure, xlsclose must be called after all the xlswrite operations are complete. If file does not exist, a new file is created.
Type: string | struct
data
Only real matrices are supported if data is a matrix.
Type: cell | mat | scalar | string
w (optional)
Worksheet name or number. If no w is specified, the first worksheet is used to store the data. Empty worksheets are added to the right of the last sheet if the worksheet number is less than what exists in file.
Type: int | string
range (optional)
Specifies the range where the data needs to be written to. If the specified range is greater than what exists in file, additional cells are created. If there is no range specified, data is added to the top left corner of the worksheet.
Type: string
itype
Interface type for xlswrite. Valid values are oml(default) and com. com interface runs the Qt/ActiveX COM libraries, which require Microsoft Excel to be installed on the machine.
Type: string

Outputs

R
A value of 1 indicates success and 0 indicates failure.
Type: integer

Examples

Write an Excel-compatible file with defaults:
values = {1, 2, 3 ; 'a', 'b', 'c' ; nan, inf, realmax};
headers = {'number','string','other'};
R = xlswrite('test.xlsx', [headers; values])
R = 1
Write an Excel-compatible file with a worksheet name specified:
values = {1, 2, 3 ; 'a', 'b', 'c' ; nan, inf, realmax};
headers = {'number','string','other'};
R = xlswrite('test.xls', [headers; values], 'data')
R = 1
Write an Excel-compatible file with a worksheet number and range specified, using 'com' interface:
values = {1, 2, 3 ; 'a', 'b', 'c' ; nan, inf, realmax};
headers = {'number','string','other'};
R = xlswrite('test.xls', [headers; values], 3, 'B3:D6', 'interface', 'com')
R = 1
Write an Excel-compatible file with defaults using structure from xlsopen/xlsclose:
values = {1, 2, 3 ; 'a', 'b', 'c' ; nan, inf, realmax};
headers = {'number','string','other'};
file = xlsopen('test.xlsx', 1);
xlswrite(file,  [headers; values], 'sheet1');
xlsclose(file);