LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

I need to read and write to excel file cvi

I have found that the ActiveX is quite cumbbersome and counter intuitive as well as anyone who is beginnning to do basic tasks with excel file. I have read through all the posts (some 300) and tried all the included demo's. I am looking for a 'helloworld!' program to the most basic task. I do not need to make a graph or any elaborate equations. I would like to take an existing excel file and read a string that is located in one cell.
In sudo code I imagine something like this...

OpenFile("somefile.xls"); //open somefile.xls
MySting = ReadData("E1"); //read data located at cell location E1 and store in MyString
CloseFile("somefile.xls"); //close somefile.xls

why can't life be this easy?

I would like to write data also...

OpenFile("someotherfile.xls"); //open someotherfile.xls
MyOtherString = WriteData("C4"); //write data to cell location C4 from MyOtherString
CloseFile("someotherfile.xls"); //close someotherfile.xls

I know I need an activeX server, Objects, Variants, Handles, etc. Is there anyone out there who could explain how to do these remedial tasks?

Jeremy
0 Kudos
Message 1 of 5
(6,296 Views)
Hello Jeremy,

I would suggest looking at the shipping example: excel2000dem.prj. This example is located in the following directory for CVI 7.x: ..\National Instruments\CVI7.x\samples\activex\excel.

For previous version of CVI, the example should be located in the following directory: ..\MeasurementStudio\cvi\samples\activex\excel.

This example documents how to open, close, write and read data from xls files. In particular, this example demonstrates multiple ways you can read and write data. For your situation, there are two documented ways to read data from specific cells: (1) Get each cell value in Range one at a time using an offset from the range's top left cell, and (2)Get Range as a SAFEARRAY inside a VARIANT.

Take a look at that example, and it should help.


Thanks Jeremy!
Wendy L
LabWindows/CVI Developer Newsletter
0 Kudos
Message 2 of 5
(6,271 Views)
Thanks Wendy. As I said in my previous post, I have gone through all the activeX demos which includes the one you mentioned. These demos aren't realy that good (for me). This topic has been discussed quite a bit on the forum. The answer always points back to the demo, which is not working. Does anybody realy know what the demo is doing line for line. Especially the VARIANT part.

Jeremy
0 Kudos
Message 3 of 5
(6,249 Views)
Jeremy,

Excel is obviously a large, complex application with a lot of functionality. As such, the ActiveX interface is also large and complex.

To help with this, CVI includes an instrument driver called ExcelReport that contains a simplified ActiveX interface for Excel. You can find the ExcelReport instrument driver under CVI\samples\ActiveX\excel\ (CVI 6.0) or CVI\toolslib\ActiveX\excel\ (CVI 7.0 or later). The version in CVI 7.0 and later includes some bug fixes and a little better documentation.

This interface was designed to be simpler to use and to include some of the most common basic functionality. Since it does not include all Excel functionality, it is designed so that object references can be used with the full Excel 2000 ActiveX interface for more complex operations.

You can find an example for ExcelReport under CVI\samples\ActiveX\excel\excelreportdemo.prj.

Here is a snippet of code that reads and writes a cell via the ExcelReport instrument driver:

ExcelRpt_ApplicationNew (1, &appHandle);
ExcelRpt_WorkbookOpen (appHandle, "somefile.xls", &workbookHandle);
ExcelRpt_GetWorksheetFromName (workbookHandle, "Sheet1", &worksheetHandle);
ExcelRpt_GetCellValue (worksheetHandle, "A1", ExRCont_dataDouble, &value);
ExcelRpt_SetCellValue (worksheetHandle, "A2", ExRCont_dataDouble, 3.14);
CA_DiscardObjHandle (worksheetHandle);
ExcelRpt_WorkbookClose (workbookHandle, 1);
CA_DiscardObjHandle (workbookHandle);
ExcelRpt_ApplicationQuit (appHandle);
CA_DiscardObjHandle (appHandle);

Hope that helps.

-Jeff
NI
0 Kudos
Message 4 of 5
(6,217 Views)
Jeff,

This is what I was looking for more or less. I am going to play with the reportdemo.fp this week. Tomorrow I have the NI symposium to go to down in SLC. For the most part this looks like a simplistic approach without being an activex programmer programmer.

Jeremy
0 Kudos
Message 5 of 5
(6,202 Views)