actxserver excel application

Can anyone give me a simple explanation of how to use actxserver to import data from matlab. I have a working code by using loops and xlsread but the running time is way too long. All I understand about actxserver is that it will save time as it doesn't open and close excel every time it goes through the loop.
I've got this far:
hExcel = actxserver('Excel.Application');
hExcel.visible = 1; % If you want Excel visible.
hExcel.DisplayAlerts = false; % Avoid excel warning popups
Which basically just opens excel. I now need to get some data...

 Accepted Answer

Jan
Jan on 2 Dec 2011

4 Comments

from one of the topics on the forum I cam across
http://www.orient-lodge.com/node/3430
Where in this link it shows how to use ActiveX. I have 2 questions which don't seem to be covered:
1) in the command
sheet1=excel.Worksheets.get(‘Item’, ‘Sheet1’);
is it possible to obtain everything from 'Sheet1' without having to specify the range with:
range1=get(sheet1,’Range’, ‘A1’);
a1=range1.Value;
instead can you do something like
a1=sheet1.Value;
Because your not going to know the range without looking at each .xls file first, where if you have 100 .xls files it would take some time.
2) if I have several .xls files in different directories is it a simple case of making a loop and having the path name of each file and then placing it in the line below?
file = excel.Workbooks.Open(xlsfile);
ok I've covered this much so far:
excel = actxserver('Excel.Application');
excel.Visible=1
file = excel.Workbooks.Open(Filename');
sheet1=excel.Worksheets.get('Item', 'data');
rowEnd = sheet1.Range('A1').End('xlDown').Row
colEnd = sheet1.Range('A1').End('xlToRight').Column
So now I have the number of rows and the number of columns for the spreadsheet. I just need to know how to define the range of values from these numbers instead of using cell name i.e. 'A1'.
Any advice?
Yes, to loop through different files, it's better to provide the full path of the files.
To read all the data in the sheet without specifying the range, use
MyRange=Sheet.UsedRange;
MyData=MyRange.Value;
great.

Sign in to comment.

More Answers (0)

Asked:

on 2 Dec 2011

Edited:

on 23 May 2022

Community Treasure Hunt

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

Start Hunting!