I want the file to be saved in the name of the what the user has entered in a text space in my GUI. How can I do this using the wkbk.SaveAs('filename.xls') function?

 채택된 답변

Oleg Komarov
Oleg Komarov 2011년 5월 18일

2 개 추천

function saveAsExcel
% Figure
S.fh = figure('units','pixels',...
'position',[500 500 200 100],...
'menubar','none',...
'numbertitle','off',...
'resize','off');
% Editbox
S.ls = uicontrol('style','edit',...
'unit','pix',...
'position',[10 50 180 40],...
'fontsize',14,...
'string','');
% SaveAs button
S.pb = uicontrol('style','push',...
'units','pix',...
'position',[30 10 140 30],...
'fontsize',14,...
'string','Save As',...
'callback',@pb_call);
function pb_call(varargin)
% First open an Excel Server
Excel = actxserver('Excel.Application');
% set(Excel, 'Visible', 1);
% Insert a new workbook
Workbooks = Excel.Workbooks;
Workbook = invoke(Workbooks, 'Add');
% Make the second sheet active
Sheets = Excel.ActiveWorkBook.Sheets;
sheet1 = get(Sheets, 'Item', 1);
invoke(sheet1, 'Activate');
% Get a handle to the active sheet
Activesheet = Excel.Activesheet;
% Put a MATLAB array into Excel
A = [1 2; 3 4];
ActivesheetRange = get(Activesheet,'Range','A1:B2');
set(ActivesheetRange, 'Value', A);
Workbook.SaveAs([cd '\' get(S.ls,'string') '.xlsx'])
% Quit Excel
invoke(Excel, 'Quit');
% End process
delete(Excel);
end
end

댓글 수: 4

Mel
Mel 2011년 5월 18일
Can you please explain the syntax for this (ie: what does each section stand for):
Workbook.SaveAs([cd '\' get(S.ls,'string') '.xlsx'])
Oleg Komarov
Oleg Komarov 2011년 5월 18일
You can highlight each part and execute with F9 or righ-click, nevertheless:
cd: list current directory (char)
get(S.lx,'string'): access handle contained in S.ls which points to the editbox (should have renamed to S.eb) and retrieve the value associated with the property 'string'.
'.xlsx': just the extension of the file
Concatenate everything to create the full path where to place the new excel.
Himanshu Verma
Himanshu Verma 2021년 11월 8일
How can I save the excel as '.htm' using Actxserver?
I have exported some text and images to different tabs in an excel sheet using Actxserver. If I manually open the sheet and save it as '.htm', it is possible and viewable. But I want to automate it and save the sheet as .htm using MATLAB only. Can you help me?
Cris LaPierre
Cris LaPierre 2021년 11월 11일
Answered here.

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

추가 답변 (0개)

카테고리

태그

질문:

Mel
2011년 5월 18일

댓글:

2021년 11월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by