Write Microsoft Excel spreadsheet file


  • xlswrite(filename,A) example
  • xlswrite(filename,A,sheet)
  • xlswrite(filename,A,xlRange)
  • xlswrite(filename,A,sheet,xlRange) example
  • status = xlswrite(___)
  • [status,message] = xlswrite(___)



xlswrite(filename,A) writes array A to the first worksheet in Excel® file, filename, starting at cell A1.

xlswrite(filename,A,sheet) writes to the specified worksheet.

xlswrite(filename,A,xlRange) writes to the rectangular region specified by xlRange in the first worksheet of the file.


xlswrite(filename,A,sheet,xlRange) writes to the specified sheet and range, xlRange.

status = xlswrite(___) returns the status of the write operation, and can include any of the input arguments in previous syntaxes. When the operation is successful, status is 1. Otherwise, status is 0.

[status,message] = xlswrite(___) additionally returns any warning or error message generated by the write operation in structure message.


expand all

Write Data to a Spreadsheet

Write a 7-element vector to an Excel file, testdata.xlsx.

filename = 'testdata.xlsx';
A = [12.7, 5.02, -98, 63.9, 0, -.2, 56];

Write Data to a Specific Sheet and Range in a Spreadsheet

Write mixed text and numeric data to an Excel file, testdata.xlsx, starting at cell E1 of Sheet2.

filename = 'testdata.xlsx';
A = {'Time','Temperature'; 12,98; 13,99; 14,97};
sheet = 2;
xlRange = 'E1';

Input Arguments

expand all

filename — Name of file to writestring

Name of file to write, specified as a string.

If filename does not exist, xlswrite creates a file, determining the format based on the specified extension. To create a file compatible with Excel 97-2003 software, specify an extension of .xls. To create files in Excel 2007 formats, specify an extension of .xlsx, .xlsb, or .xlsm. If you do not specify an extension, xlswrite uses the default, .xls.

Example: 'myFile.xlsx'

A — Data to writematrix | cell array

Data to write, specified as a two-dimensional numeric or character array, or, if each cell contains a single element, a cell array.

If A is a cell array containing something other than a scalar numeric or a string, then xlswrite silently leaves the corresponding cell in the spreadsheet empty.

The maximum size of array A depends on the associated Excel version. For more information on Excel specifications and limits, see the Excel help.

Example: [10,2,45;-32,478,50]

Example: {92.0,'Yes',45.9,'No'}

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char | cell

sheet — Worksheet namestring | positive integer

Worksheet name, specified as one of the following:

  • String that contains the worksheet name. Cannot contain a colon (:). To determine the names of the sheets in a spreadsheet file, use xlsfinfo.

  • Positive integer that indicates the worksheet index.

If sheet does not exist, xlswrite adds a new sheet at the end of the worksheet collection. If sheet is an index larger than the number of worksheets, xlswrite appends empty sheets until the number of worksheets in the workbook equals sheet. In either case, xlswrite generates a warning indicating that it has added a new worksheet.

xlRange — Rectangular portion of the worksheet to writestring

Rectangular portion of the worksheet to write, specified as a string.

Specify xlRange using the syntax 'C1:C2', where C1 and C2 are two opposing corners that define the region to write. For example, 'D2:H4' represents the 3-by-5 rectangular region between the two corners D2 and H4 on the worksheet. The xlRange input is not case sensitive, and uses Excel A1 reference style (see Excel help). xlswrite does not recognize named ranges.

  • If you do not specify sheet, then xlRange must include both corners and a colon character, even for a single cell (such as 'D2:D2'). Otherwise, xlswrite interprets the input as a worksheet name (such as 'D2').

  • If you specify sheet, then xlRange can specify only the first cell (such as 'D2'). xlswrite writes input array A beginning at this cell.

  • If xlRange is larger than the size of input array A, Excel software fills the remainder of the region with #N/A. If xlRange is smaller than the size of A, then xlswrite writes only the subset that fits into xlRange to the file.

Output Arguments

expand all

status — Status of the write operation1 | 0

Status of the write operation, returned as either 1 (true) or 0 (false). When the write operation is successful, status is 1. Otherwise, status is 0.

message — Error or warning generated during the write operationstructure array

Error or warning generated during the write operation, returned as a structure array containing two fields:

messageText of the warning or error message, returned as a string.
identifierMessage identifier, returned as a string.

More About

expand all


  • If your system has Microsoft® Office 2003 software, but you want to create a file in an Excel 2007 format, install the Office 2007 Compatibility Pack.

  • Excel and MATLAB® can store dates as strings (such as '10/31/96') or serial date numbers (such as 729329). If your array A includes serial date numbers, convert the dates to strings using datestr before calling xlswrite. Alternatively, see Import and Export Dates to Excel Files.

  • To write data to Excel files with custom formats (such as fonts or colors), access the Windows® COM server directly using actxserver rather than xlswrite. For example, Technical Solution 1-QLD4K uses actxserver to establish a connection between MATLAB and Excel, writes data to a worksheet, and specifies the colors of the cells.


Excel converts Inf values to 65535. MATLAB converts NaN values to empty cells.

If your system does not have Excel for Windows, or if the COM server (part of the typical installation of Excel) is unavailable, then the xlswrite function:

  • Writes array A to a text file in comma-separated value (CSV) format.

  • Ignores the sheet and xlRange arguments.

  • Generates an error when input array A is a cell array.

Was this topic helpful?