LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Read Excel cells - part2

This is a follow up from my question last week. I am back to trying to figure out how to read excel data cells and store the values into variables. I've looked through the sample files and copied the code from excel2000demo.cws. I can't actually run this demo since I don't have excel on my system. I need to be able to read the .xls without having Excel loaded. I've put ReadDataFromExcel into my program but when I try and run it, I get an error code(negative) at the Excel_WorksheetRange statement. The error is invalid handle. The line is:

 

    error = Excel_WorksheetRange (ExcelWorksheetHandle, NULL, MyCellRangeV, CA_DEFAULT_VAL, &ExcelRangeHandle);

I don't really understand what the parameters are or should be and if certain things need to be defined elsewhere.

 

It also seems like I should have code to tell it what file to look into. The demo uses Excel_WorkbooksOpen. Is that ok to use with no Excel? I did try adding:

                GetProjectDir (fileName);
                strcat(fileName, "\\exceldem.xls");
                error = Excel_WorkbooksOpen (ExcelWorkbooksHandle, NULL, fileName, CA_DEFAULT_VAL,
                                             CA_DEFAULT_VAL, CA_DEFAULT_VAL,
                                             CA_DEFAULT_VAL, CA_DEFAULT_VAL,
                                             CA_DEFAULT_VAL, CA_DEFAULT_VAL,
                                             CA_DEFAULT_VAL, CA_DEFAULT_VAL,
                                             CA_DEFAULT_VAL, CA_DEFAULT_VAL,
                                             CA_DEFAULT_VAL, &ExcelWorkbookHandle);

 

I get invalid handle errors here too.

 

Mike

 

0 Kudos
Message 1 of 7
(4,233 Views)

Hi Mike,

 

I  do not think you can use the library without Excel installed.

The library is an API interface to Excel, it does not contain the Excel "engine".

 

The biggest clue is that, the first function you need to call is to start the Excel application and get an application handle.

Without the handle you cannot use other functions, and without the Excel installed you cannot start the application.

S. Eren BALCI
IMESTEK
0 Kudos
Message 2 of 7
(4,223 Views)

Unfortunately you cannot run this example without having installed Excel on your system, since the instrument driver it relies on is nothing but an ActiveX interface to the local Excel application.

In effect, if you look into the source code for this example, you will notice that the very first operation performed is to connect to Excel: this instruction will fail if the application is not installed, so all the following code cannot be executed. You will notice that every opening function in the source code relies on an handle provided from a preceding statement, in a continuous chain whose first element is exactly the connection to the Excel app: this is valid for ExcelWorksheetHandle, ExcelWorbookHandle and all the handles used in the code.

 

 

Edit: Eren has come first! Smiley Wink

Message Edited by Roberto Bozzolo on 04-28-2010 07:22 AM


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 3 of 7
(4,222 Views)

OK, so forgetting about the demo for now, is there any way to read cells from an excel spreadsheet without having excel or does it have to be converted to a CSV file?

 

Mike

0 Kudos
Message 4 of 7
(4,197 Views)

XLS is not a simple file format.

Even a completely empty xls file weighs 13.5 KB

So it is not easy to parse it byte-by-byte and find where the actual information is.

 

However, CSV is like normal text file. No overhead.

You can open it as an ASCII file and read directly line-by-line.

Each excel row corresponds to a line and column data is seperated by comma character.

 

So converting to CSV can be a solution to your problem. 

S. Eren BALCI
IMESTEK
0 Kudos
Message 5 of 7
(4,180 Views)

I would like to elaborate briefly on ebalci's accurate comments. XLS files are binary files, which means you cannot, for example, open them in a text editor and understand what is inside the file. In addition to this, there is a lot of additional information there which explains the formatting of the cells, etc. in the file. All together, this makes is very difficult to read true XLS files from c. In a brief google search, I found the following thread which discusses a similar topic (What is a simple and reliable C library for working with Excel files?). Their conclusion was that there are a few libraries for this, but all are fairly complicated to use correctly.

 

In comparison, as ebalci said, CSV files are ASCII text files and can be read and parsed easily.

National Instruments
Applications Engineer
0 Kudos
Message 6 of 7
(4,155 Views)

Thanks for your inputs.

 

Mike

0 Kudos
Message 7 of 7
(4,150 Views)