MATLAB Answers

Save Matlab data in Excel with ActiveX

9 views (last 30 days)
Joanna Przeworska
Joanna Przeworska on 7 May 2021
Commented: Joanna Przeworska on 17 May 2021 at 10:50
I need your help as I am not very experienced in linking with Excel through ActiveX. Below I attach the code I managed to write so far (based on suggestions from Internet). My goal is to use template.xlsx which contains desired formatting, write values inside and save everything under different name, e.g. file.xlsx.
I would appreciate if you help me complete this code.
% Specify file name
file = 'P:\WPM\CCB\Notatka\RF\template.xlsx'; % This must be full path name
% Open Excel Automation server
Excel = actxserver('Excel.Application');
Workbooks = Excel.Workbooks;
% Make Excel visible
% Open Excel file
% Specify sheet number, data, and range to write to
data = SECTORS.PNFS.Total{1:end,1:end}; % use a cell array if you want both numeric and text data
range = 'B9:L77';
% Make the first sheet active
Sheets = Excel.ActiveWorkBook.Sheets;
sheet1 = get(Sheets, 'Item', sheetnum);
invoke(sheet1, 'Activate');
Activesheet = Excel.Activesheet;
% Put MATLAB data into Excel
ActivesheetRange = get(Activesheet,'Range',range);
set(ActivesheetRange, 'Value', data);

Accepted Answer

Cris LaPierre
Cris LaPierre on 7 May 2021
Joanna Przeworska
Joanna Przeworska on 17 May 2021 at 10:50
Dear Cris,
That's enough. It helped me a lot. Thank you very much.
Kind regards,

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by