ni.com is currently undergoing scheduled maintenance.

Some services may be unavailable at this time. Please contact us for help or try again later.

LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

write data to cell in excel

Solved!
Go to solution

Hi,

I am trying to write data into a particular cell in an excel. when i am looking into example program i felt bit difficult to understand.

i have wriiten code to read data from particular cell which works well. I am facing difficulties in writing data to cell. pls someone help.

// to read a data from cell which works

           status = Excel_RangeGetItem (rangeHandle, NULL, CA_VariantInt (i+1), CA_VariantInt (j+1), &MyVariant);
         
            // Get the DISPATCH pointer
            status = CA_VariantGetDispatch (&MyVariant, &MyDispatch);
           
            // Create a new Range Object from DISPATCH pointer
            status = CA_CreateObjHandleFromIDispatch (MyDispatch, 0, &ExcelSingleCellRangeHandle);
           
            // Get the value of the Single Cell Range
            status = Excel_GetProperty (ExcelSingleCellRangeHandle, NULL, Excel_RangeValue2, CAVT_VARIANT, &MyVariant);
          
           status= CA_VariantGetCString(&MyVariant,&val);   
 strcpy(cellvalue,val);

// to write data into a cell

 status = Excel_RangeSetItem (rangeHandle, NULL, CA_VariantInt (i+1), CA_VariantInt (j+1), MyVariant);
         
            status = CA_VariantSetDispatch (&MyVariant, &MyDispatch);
           
            status = CA_CreateObjHandleFromIDispatch (MyDispatch, 0, &ExcelSingleCellRangeHandle);
   
            status = Excel_SetProperty (ExcelSingleCellRangeHandle, NULL, Excel_RangeValue2, CAVT_VARIANT, &MyVariant);
         
   status= CA_VariantSetCString(&MyVariant,writedata); 

i know i am doing some mistake in the above code.

0 Kudos
Message 1 of 6
(5,182 Views)

Can someone help. i need to set a value in the ith row and jth column in an excel as i did for reading cell value.

 

void write_cell_value(int i, int j, char *cellvalue)


{


 static VARIANT MyVariant;
    
 status= CA_VariantSetCString(&MyVariant,cellvalue); 
 
  status = Excel_RangeSetItem (rangeHandle, NULL, CA_VariantInt (i), CA_VariantInt (j), MyVariant);  
     
}

0 Kudos
Message 2 of 6
(5,157 Views)

As far as I can see your write_cell_Value function only needs to receive 'rangeHandle' parameter, then it should work correctly. Also cut the static keyword from the definition of MyVariant and free it at the end with CA_VariantClear.

Do you receive any error when running the program?



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 6
(5,154 Views)

I removed static keyword and it works. thank  you very much  Roberto.

 

 

0 Kudos
Message 4 of 6
(5,147 Views)

CA_VariantIsEmpty function can be of help in this respect: if not empty then read the value.



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 5 of 6
(5,142 Views)
Solution
Accepted by topic author ammukrish

Thank you very much Roberto. You have solved my issue.

0 Kudos
Message 6 of 6
(5,129 Views)