Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Xlsread while Excel File is open?

Subject: Xlsread while Excel File is open?

From: Alaa Elwany

Date: 4 Jan, 2012 17:24:08

Message: 1 of 6

Hi,

I am using xlsread to import data from Excel (And subsequently, xlswrite to export back into Excel after i run my code).

My only problem is:
For some technical reason beyond explanation, I need to be having the Excel File from which I import the data OPEN while running the code! xlsread wouldn't do that, and needs me to close the file in order to be able to import from it.

Is there anyway around this? Anyway that i can have xlsread/xlswrite to work when the Excel file is open??

Thanks,

Alaa

Subject: Xlsread while Excel File is open?

From: Barry Williams

Date: 4 Jan, 2012 20:15:10

Message: 2 of 6

"Alaa Elwany" <alaa_elwany@yahoo.com> wrote in message <je21vo$oci$1@newscl01ah.mathworks.com>...
> Hi,
>
> I am using xlsread to import data from Excel (And subsequently, xlswrite to export back into Excel after i run my code).
>
> My only problem is:
> For some technical reason beyond explanation, I need to be having the Excel File from which I import the data OPEN while running the code! xlsread wouldn't do that, and needs me to close the file in order to be able to import from it.
>
> Is there anyway around this? Anyway that i can have xlsread/xlswrite to work when the Excel file is open??
>
> Thanks,
>
> Alaa

Have you tried using xlsread with an open file? I do that regularly and have not had problems in the past. Running xlswrite with an open file won't work. Maybe someone knows a workaround. I don't.
Barry

Subject: Xlsread while Excel File is open?

From: Alaa Elwany

Date: 4 Jan, 2012 21:05:09

Message: 3 of 6

"Barry Williams" <barry.r.williamsnospam@saic.com> wrote in message <je2c0e$t7m$1@newscl01ah.mathworks.com>...
> "Alaa Elwany" <alaa_elwany@yahoo.com> wrote in message <je21vo$oci$1@newscl01ah.mathworks.com>...
> > Hi,
> >
> > I am using xlsread to import data from Excel (And subsequently, xlswrite to export back into Excel after i run my code).
> >
> > My only problem is:
> > For some technical reason beyond explanation, I need to be having the Excel File from which I import the data OPEN while running the code! xlsread wouldn't do that, and needs me to close the file in order to be able to import from it.
> >
> > Is there anyway around this? Anyway that i can have xlsread/xlswrite to work when the Excel file is open??
> >
> > Thanks,
> >
> > Alaa
>
> Have you tried using xlsread with an open file? I do that regularly and have not had problems in the past. Running xlswrite with an open file won't work. Maybe someone knows a workaround. I don't.
> Barry

You are right Barry. xlsread works with an open Excel file, unlike "Xlswrite".

Does anyone know a workaround for "xlswrite"?

Thanks for your response,

Subject: Xlsread while Excel File is open?

From: ImageAnalyst

Date: 4 Jan, 2012 21:53:43

Message: 4 of 6

Alaa:
Yes I think you'd be able to do it with ActiveX. Will the workbook
you want to open be already open in Excel, or will none or a different
workbook be open in Excel?

How good are you at ActiveX programming? It's not for the faint of
heart or weak programmer. It takes some research to figure out
exactly what methods you need to call to do certain things. These web
sites may help:
http://msdn.microsoft.com/en-us/library/bb242656%28office.12%29.aspx
http://msdn.microsoft.com/en-us/library/aa272268%28office.11%29.aspx
http://support.microsoft.com/?scid=kb;EN-US;302094

ImageAnalyst

Subject: Xlsread while Excel File is open?

From: Alaa Elwany

Date: 5 Jan, 2012 16:29:08

Message: 5 of 6

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <608c9343-70cf-4bac-9afd-d02a456bee79@x20g2000yqe.googlegroups.com>...
> Alaa:
> Yes I think you'd be able to do it with ActiveX. Will the workbook
> you want to open be already open in Excel, or will none or a different
> workbook be open in Excel?
>
> How good are you at ActiveX programming? It's not for the faint of
> heart or weak programmer. It takes some research to figure out
> exactly what methods you need to call to do certain things. These web
> sites may help:
> http://msdn.microsoft.com/en-us/library/bb242656%28office.12%29.aspx
> http://msdn.microsoft.com/en-us/library/aa272268%28office.11%29.aspx
> http://support.microsoft.com/?scid=kb;EN-US;302094
>
> ImageAnalyst

Thanks a lot ImageAnalyst.
The Excel workbook will, indeed, be already open in Excel when I need to write to it.

Unfortunately I am not good with ActiveX programming :-( It does seem like it's the "only' way out? I'll try to make use of the links you sent. Apparently, there is no easy way to do it from within Matlab.

Thanks a lot for your feedback,

Best,
Alaa

Subject: Xlsread while Excel File is open?

From: ImageAnalyst

Date: 5 Jan, 2012 17:54:18

Message: 6 of 6

Alaa:
Start with code like this:

% Launch Excel.
Excel = actxserver('Excel.Application');
excelFullFileName = 'D:\yourfolder\MATLAB Results for my test.xlsx';

% Prepare proper filename extension.
% Get the Excel version because if it's version 11 (Excel 2003) the
file extension should be .xls,
% but if it's 12.0 (Excel 2007) then we'll need to use an extension
of .xlsx to avoid nag messages.
excelVersion = str2double(Excel.Version);
if excelVersion < 12
excelExtension = '.xls';
else
excelExtension = '.xlsx';
end

% Determine the proper format to save the files in. It depends on the
extension (Excel version).
switch excelExtension
case '.xls' %xlExcel8 or xlWorkbookNormal
xlFormat = -4143;
case '.xlsb' %xlExcel12
xlFormat = 50;
case '.xlsx' %xlOpenXMLWorkbook
xlFormat = 51;
case '.xlsm' %xlOpenXMLWorkbookMacroEnabled
xlFormat = 52;
otherwise
xlFormat = -4143;
end
% Add a new workbook.
% ExcelWorkbook = Excel.workbooks.Add;

% Open up the workbook named in the variable fullFileName.
invoke(Excel.Workbooks,'Open',excelFullFileName);
% Save this workbook we just opened.
ExcelWorkbook.SaveAs(excelFullFileName, xlFormat);
Excel.Quit;

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us