Save Matlab data in Excel with ActiveX

조회 수: 37 (최근 30일)
Joanna Przeworska
Joanna Przeworska 2021년 5월 7일
댓글: Joanna Przeworska 2021년 5월 17일
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);

채택된 답변

Cris LaPierre
Cris LaPierre 2021년 5월 7일
  댓글 수: 3
Cris LaPierre
Cris LaPierre 2021년 5월 14일
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 2021년 5월 17일
Dear Cris,
That's enough. It helped me a lot. Thank you very much.
Kind regards,
JP

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by