Save Matlab data in Excel with ActiveX

38 visualizzazioni (ultimi 30 giorni)
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);

Risposta accettata

Cris LaPierre
Cris LaPierre il 7 Mag 2021
  3 Commenti
Cris LaPierre
Cris LaPierre il 14 Mag 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 il 17 Mag 2021
Dear Cris,
That's enough. It helped me a lot. Thank you very much.
Kind regards,
JP

Accedi per commentare.

Più risposte (0)

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by