Save Matlab data in Excel with ActiveX

37 visualizaciones (últimos 30 días)
Joanna Przeworska
Joanna Przeworska el 7 de Mayo de 2021
Comentada: Joanna Przeworska el 17 de Mayo de 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);

Respuesta aceptada

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

Iniciar sesión para comentar.

Más respuestas (0)

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by