Naming sheets with xlswrite. As simple as that. I cannot find the way.
Thank you!

 채택된 답변

Guillaume
Guillaume 2019년 7월 19일

0 개 추천

Have you looked at the documentation of xlswrite?
xlswrite(filename,A,sheet) writes to the specified worksheet.

댓글 수: 4

kionstan
kionstan 2019년 7월 19일
Yes, thank you. But what I want is to give a name (different than the default one: 'sheet 1' and so on) to the sheet where I will write.
You're not very clear. xlswrite will create a sheet with whichever name you want.
If you want to rename an existing sheet, it's not something that can be done with xlswrite as it's not its purpose, but that can be done using the COM interface.
excel = actxserver('Excel.Application');
workbook = excel.Workbooks.Open('fullpathtotheexcelfile.xlsx');
workbook.Worksheets.Item('CurrentNameOfSheet').Name = 'NewNameOfSheet';
workbook.Save;
excel.Quit;
kionstan
kionstan 2019년 7월 19일
OK, I'm sorry, now I can see. As in the examples the field 'sheet' is always filled with a positive integer (indicating the worksheet index), I didn't realize that actually you can assign a name too, creating in such way a new worksheet.
However, when I do that, I create as many new sheets as I need with the name that I want, but I cannot rid of the first default sheet with index 1. Any solution?
No, if the workbook doesn't exist already xlswrite will typically create it with 3 worksheets named 'Sheet1' to 'Sheet3'. This can be reduced to just one sheet in the options of excel but excel does not allow the creation of workbooks without any sheet.
You can delete that worksheet afterward with the COM interface:
excel = actxserver('Excel.Application');
excel.DisplyAlerts = false;
workbook = excel.Workbooks.Open('fullpathtotheexcelfile.xlsx');
workbook.Worksheets.Item('Sheet1').Delete;
workbook.Save;
excel.Quit;

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

kionstan
kionstan 2019년 7월 19일

0 개 추천

Thank you! It worked well.

카테고리

질문:

2019년 7월 19일

답변:

2019년 7월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by