Excel SaveAs method and naming conventions

I was curious if there was a built in WB.SaveAs method and how to use it.
I'm currently using WB.Save to highlight the difference between two excel files, but wanted to have not override the original document.
Also would like the files' names to end differently as well by using the folder's names at end, as excel doesn't want to open up two files with the same names.

 採用された回答

Image Analyst
Image Analyst 2022 年 6 月 21 日
編集済み: Image Analyst 2022 年 6 月 21 日

0 投票

There is a SaveAs method. You can record a macro in Excel to see how it's typically used. Or see this:
You might also like to look at the functions strrep, fileparts, and sprintf to create new filenames.
I'm also attaching an Excel_utils class with a bunch of useful functions.

6 件のコメント

Emily
Emily 2022 年 6 月 27 日
Hi, I read through the link and some other similar questions posted here and tried to use the code below, but still having trouble saving it as a new excel file.
WB.SaveAs([pwd,'\',xls_fname,'_',file_index,'.xlsx'])
Image Analyst
Image Analyst 2022 年 6 月 28 日
Attached is a full demo.
Emily
Emily 2022 年 6 月 28 日
Hi, I looked at the code written and ran the example problem.
But I'm still having trouble with the saveas portion
This is my current code
NewExcel=char(pwd,'\',fname,'_',num2str(file_index),'.xlsx');
NewExcel=reshape(NewExcel',1,[]);
WB.SaveAs(NewExcel);
Maybe the way I wrote NewExcel is wrong?
Walter Roberson
Walter Roberson 2022 年 6 月 29 日
Why are you doing that reshape() of the file name?
Note: I recommend that you use fullfile()
Image Analyst
Image Analyst 2022 年 6 月 29 日
Try it this way:
baseFileName = sprintf('%s_%2.2d.xlsx', fname, file_index);
fullFileName = fullfile(pwd, baseFileName);
fprintf('Saving "%s".\n', fullFileName);
WB.SaveAs(fullFileName);
Emily
Emily 2022 年 6 月 29 日
Thank you everyone it works now!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeEnvironment and Settings についてさらに検索

タグ

質問済み:

2022 年 6 月 21 日

コメント済み:

2022 年 6 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by