Importing data from excel with formula
Show older comments
I want to import the data from excel sheet ensuring that the logic behind the sheet is not lost. So the formula remains intact. What is the procedure for that?
File attached.
Accepted Answer
More Answers (2)
Image Analyst
on 26 Nov 2019
Try this well commented, fully functioning demo I made up. Change the name of the workbook to whatever you're using instead of "Example.xlsx".
% Launch an Excel server using ActiveX (Windows ONLY).
excelObject = actxserver('Excel.Application');
% Create the filename of the existing workbook.
fullFileName = fullfile(pwd, 'Example.xlsx');
% Make sure the file exists.
if ~isfile(fullFileName)
errorMessage = sprintf('The workbook file does not exist:\n%s', fullFileName);
uiwait(errordlg(errorMessage));
return;
end
% Open the workbook from disk.
excelWorkbook = excelObject.workbooks.Open(fullFileName);
% Excel is invisible so far. Make it visible.
excelObject.Visible = true;
% Create a string with the formula just like you'd have it in Excel.
yourFormula = '=SUM(A1..A100)'; % No spaces allowed.
% Assign the formula to the cell "B1".
excelWorkbook.ActiveSheet.Range('B1').Formula = yourFormula;
% Save the current state of the workbook.
excelWorkbook.Save;
% Close the workbook. Excel will stay open but be hidden.
% You can still see it as "Microsoft Excel" in Task Manager.
excelWorkbook.Close;
% Shut down the Excel server instance.
excelObject.Quit;
% Even after quitting, you can still see it as "Microsoft Excel" in Task Manager.
% Clear the excel object variable from MATLAB's memory.
clear('excelObject', 'excelWorkbook', 'yourFormula');
% The clear finally shuts down the server and it no longer appears in Task Manager.
fprintf('Done interacting with Excel.\n');
Image Analyst
on 15 May 2014
0 votes
I'm sure you can do it using ActiveX, though I'm not sure which ActiveX command is the correct one to use. You can pretty much do absolutely anything with Office apps using ActiveX. It's the figuring out which method to use that is the hard part since there are thousands of methods.
1 Comment
dpb
on 15 May 2014
Yeah, should've added the caveat to "can't" -- I was limiting to the "import" idea using a higher-level function.
Categories
Find more on Data Import from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!