Save Matlab data in Excel with ActiveX

44 Ansichten (letzte 30 Tage)
Joanna Przeworska
Joanna Przeworska am 7 Mai 2021
Hello,
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
Excel.Visible=1;
% Open Excel file
Workbook=Workbooks.Open(file);
% Specify sheet number, data, and range to write to
sheetnum=1;
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);

Akzeptierte Antwort

Cris LaPierre
Cris LaPierre am 7 Mai 2021
  3 Kommentare
Cris LaPierre
Cris LaPierre am 14 Mai 2021
So what is the missing piece? Saving it under a new name? Use the 'saveas' method instead of 'save'.
Workbooks.SaveAs(newfile);
If you want a solution guaranteed to work for your specific case, share your data and template files for us to use in our testing. You can attach them using the paperclip icon.
Joanna Przeworska
Joanna Przeworska am 17 Mai 2021
Dear Cris,
That's enough. It helped me a lot. Thank you very much.
Kind regards,
JP

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by