Write Microsoft Excel spreadsheet file


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



xlswrite(filename,A) writes matrix A to the first worksheet in the Microsoft® Excel® spreadsheet workbook 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 workbook. Use Excel range syntax, such as 'A1:C3'.


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

status = xlswrite(___) returns the status of the write operation, using 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.


collapse all

Write Vector to Spreadsheet

Write a 7-element vector to an Excel file.

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

Write to Specific Sheet and Range in Spreadsheet

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

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

Input Arguments

collapse all

filename — File namestring

File name, 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'

Example: 'C:\myFolder\myFile.xlsx'

A — Input matrixmatrix

Input matrix, 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. The string 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 rangestring

Rectangular range, specified as a string.

Specify xlRange using 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

collapse 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.


  • If your computer 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. A must be a numeric matrix.

    • Ignores the sheet and xlRange arguments.

More About

expand all


  • If your computer 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 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, this MathWorks Support Answer 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.

Introduced before R2006a

Was this topic helpful?