Naming sheets in exported xlsx file

Hey, I have a table of titles, that is 1 line and i columns (i changes). I export a (different) table to excel using writetable. How can I use the entries in the title table to be the names of the sheets in the xlsx file?
Thank you in advance.

 Accepted Answer

You could change the sheet name using activex server,
excel = actxserver('Excel.Application'); % # open Activex server
filepath = pwd;
filename = 'yourfileName.xls';
excelWorkBook = excel.Workbooks.Open(fullfile(filepath,filename));
excelWorkBook.Worksheets.Item(1).Name = 'your table name';
excelWorkBook.Save % # save to the same file
excelWorkBook.Close(false)
excel.Quit

3 Comments

Oded Zimron
Oded Zimron on 5 Sep 2017
Edited: Oded Zimron on 5 Sep 2017
Thank you for your response. I used your suggestion, and there is no error, but for some reason it is taking a very long time to run. Is it always like that with activex server?
How big is your file? Nevertheless, why not rename all the sheets beforehand and then write your data on it?
The code provided by KL should be very fast to run unless there is something slowing excel start up. In the past, I've had some excel add-ons really slow down the start up. So have a look at that.
Note that if you're renaming several sheets at once, then the only line that should be in the renaming loop should be the line:
excelWorkBook.Worksheets.Item(1).Name = 'your table name';
All others lines should be outside of the loop. In particular, if you're constantly starting excel (with the actxserver line) and quiting excel (with the excel.Quit line) then, yes, it's going to be slow.

Sign in to comment.

More Answers (0)

Asked:

on 4 Sep 2017

Commented:

on 5 Sep 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!