| Contents | Index |
| On this page… |
|---|
This example creates a spreadsheet, copies some data to it, and closes it. To create a workbook, type:
NET.addAssembly('microsoft.office.interop.excel');
app = Microsoft.Office.Interop.Excel.ApplicationClass;
books = app.Workbooks;
newWB = Add(books);
app.Visible = true;
Create a new sheet:
sheets = newWB.Worksheets; newSheet = Item(sheets,1);
newSheet is a System.__ComObject because sheets.Item can return different types, such as a Chart or a Worksheet. To make the sheet a Worksheet, use the command:
newWS = Microsoft.Office.Interop.Excel.Worksheet(newSheet);
Create some data and write to a range of cells:
excelArray = rand(10); newRange = Range(newWS,'A1'); newRange.Value2 = 'Data from Location A'; newRange = Range(newWS,'A3:B12'); newRange.Value2 = excelArray;
Modify cell format and name the worksheet:
newFont = newRange.Font;
newFont.Bold = 1;
newWS.Name = 'Test Data';
If this is a new spreadsheet, use the SaveAs method:
SaveAs(newWB,'mySpreadsheet.xlsx');
Close and quit:
Close(newWB); Quit(app);
The following code creates a new Word document:
NET.addAssembly('microsoft.office.interop.word');
wordApp = Microsoft.Office.Interop.Word.ApplicationClass;
wordDoc = wordApp.Documents;
newDoc = Add(wordDoc);
If you want to type directly into the document, type the MATLAB command:
wordApp.Visible = true;
Put the cursor into the document window and enter text.
To name the document myDocument.docx and save it in the My Documents folder, type:
SaveAs(newDoc,'myDocument.docx');
When you are finished, to close the document and application, type:
Save(newDoc); Close(newDoc); Quit(wordApp);
![]() | .NET Enumerations in MATLAB | .NET Generic Classes in MATLAB | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |