LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Excel read in CVI

Solved!
Go to solution

 

I just open the excel file as 'string' in CVI.

I already read http://forums.ni.com/t5/LabWindows-CVI/How-do-I-allocate-memory-for-excel-strings/m-p/754813#M37082

However, I cannot understand it  T-T because I am poor at programming.

 

Does any guys teach how to open the excel file and appear at table in CVI UI.

 

 

 

0 Kudos
Message 1 of 6
(8,811 Views)

I simply don't understand what do you mean by "open the excel file as string" Smiley Surprised

 

CVI come from many years with an ActiveX interface that permits to read and write Excel files: you can see the corresponding example "excel2000dem" in folder <CVIsamplesFolder>\ActiveX\Excel. With this instrument you are able to open a file and read its data, displaying them in a table on your panel.

 

On top of this interface the Excel Report instrument was built, which is the one the discussion you linked refers to. Again, CVI comes with examples of using it and there are several discussions in the forums on this subject. You may particulary want to explore ExcelRpt_ReadDataToTableControl function.



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?
Message 2 of 6
(8,803 Views)

Sorry I just want to say "open the string data in excel"

(because I am not using English as my first language, Sorry for my poor english)

 

However, I already see the ExcelRpt_ReadDataToTableControl function.... But I am not understand well.

Also, the example, excel200dem, can read the data after launching excel.

 

Simply, I have a excel file with some letters and I don't know exact area such as A1:A10.

Besides, I want to just load the string data in excel as a one variable of CVI and write on table of CVI without launching Excel.

 

Can ActiveX do like this?

If it is so, please give me a matrial about ActiveX because I do not have any idea about ActiveX.

Thanks

 

0 Kudos
Message 3 of 6
(8,798 Views)

Hi! Babemin,

 

You need to study the example CVI project "excel2000dem.prj" that comes with your LabWindows/CVI.  Closely investigate the function "ReadDataFromExcel(....)" in the C program "excel2000dem.c".  The first method in this function read each individual cell in the opened Excel file and print out the value of that cell directly on screen.  This example shows you how to read multiple double values from an Excel file "exceldem.xls".  If you don't study this project, you will not be able to complete your task.

 

 

 

Assuming you understand the example program already, open this "exceldem.xls" file manually and add a text "myText1" to cell B35.  Close and save the excel file.

 

 

You can then add the following code into the function "ReadDataFromExcel(....)" in the C program "excel2000dem.c" before the label "Error:".  Be sure to add a new declaraction "char *str;" in the function. 

 

 

---------------------------------------------------------------------------------------------------------------------------------------------------------------

 

// Open and activate one cell only
    error = CA_VariantSetCString (&MyCellRangeV, "B35");
    error = Excel_WorksheetRange (ExcelWorksheetHandle, NULL, MyCellRangeV, CA_DEFAULT_VAL, &ExcelSingleCellRangeHandle);
    if (error<0) goto Error;

    // Make range Active
    error = Excel_RangeActivate (ExcelSingleCellRangeHandle, &ErrorInfo, NULL);
    if (error<0) goto Error;

            // Get the value of the Single Cell Range
            error = Excel_GetProperty (ExcelSingleCellRangeHandle, &ErrorInfo, Excel_RangeValue2, CAVT_VARIANT, &MyVariant);
            if (error<0) goto Error;

            if (!CA_VariantHasCString(&MyVariant))
            {
                MessagePopup(APP_WARNING, "Values returned were not of type character string.");
                goto Error;
            }
            
            error = CA_VariantGetCString(&MyVariant, &str);
            if (error<0) goto Error;

            // Free Variant element
            CA_VariantClear(&MyVariant);
           
            printf("%s\n ", str);

---------------------------------------------------------------------------------------------------------------------------------------------------------------

 

 

Now run the project again.  Click "Read Data" in the front panel and you'll see it reads and prints "myText1" on the screen.

 

This is the method to read a text string from a known cell position in an Excel file. 

 

Message 4 of 6
(8,786 Views)

Thanks. It helps me very much!

 

But... Can I ask one more question?

 

I want to read other sheets like sheet2 in same excel file, and I find one function in excel2000.h

 


HRESULT CVIFUNC Excel_WorksheetMove (CAObjHandle objectHandle,
ERRORINFO *errorInfo, VARIANT before,
VARIANT after)
{
HRESULT __result;
unsigned int __paramTypes[] = {CAVT_VARIANT, CAVT_VARIANT};

__result = CA_MethodInvokeEx (objectHandle, errorInfo,
&Excel_IID_Worksheet, 0x27D, CAVT_EMPTY,
NULL, 2, __paramTypes, before, after);

return __result;
}

 

 

 

But, I cannot understand this function,

How can I use this function?

 

0 Kudos
Message 5 of 6
(8,770 Views)
Solution
Accepted by topic author BABEMIN

This is not the correct function to use. 

DCL9000 already answered the same question recently: look at his post here

 



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 6 of 6
(8,763 Views)