LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

writing strings to excel

I am looking for a solution to writing strings to excel.  I have looked at the ExcelReport.fp and the functions there are good, except that it seems you can't dynamically tell the function which cell you want to write into.  I have been looking at SetCellValue and RangeSetValue but they both don't seem to do it for me.  What I would really like to be able to do, because I have already done this for ints, is use the int counter I have that tells me which column I write my data into, and use that to tell me which column I write into.  To do the data I rewrote one of the functions from the Excel 2000 example given with Labwindows, but I can't seem to manipulate that function to be able to write strings.  Any help is greatly appreciated.  Thanks.
0 Kudos
Message 1 of 7
(6,144 Views)
Hi Nietsneknarf,

Will Excel_RangeSetItem work for you?
Cheers,

David Goldberg
National Instruments
Software R&D
0 Kudos
Message 2 of 7
(6,113 Views)

If I follow your question, perhaps the following code could be re-packaged into a function that does the job for you:

char cell[8];
char col;  // this is used as number
int row;

col = 'B';  // single quotes - this is not a string
row = 2;
Fmt(cell, "%s<%c%d", col, row);
ExcelRpt_SetCellValue (ws_handle, cell, ExRConst_dataString, string);

Note that if you ever have more than 26 columns, this won't work -- the col variable won't work beyond Z, as it can't be 'AA'. You might want to use the "RnCm" notation (e.g. R2C2 instead of B2). However, I'm unsure if this format is supported by the SetCellValue function; a quick test of that notation gave me an error. (If anyone can confirm this question, please post!)

The nice thing about this char version of col is that you can easily do math with it. You can increment it, e.g. col = col +2, or col++  (Watch your types, and check its within range, though!) You probably will need to supply two integer arguments (for row and column) and add a zero-based integer value cast to char for the column number to the char value 'A'.

Hope this helps,

--Ian

Message 3 of 7
(6,108 Views)
Just as a check to make sure that I understand you, if I say col++, then the col variable goes from B to C correct?  If so that is exactly what I want to do.  Thanks, going to implement this and come back with good or bad news. 
-Ben
0 Kudos
Message 4 of 7
(6,106 Views)

Yes, as a standalone statement,

col++;

is the same as

col = col+1;

or for that matter

col += 1;

The increment operator (++) and decrement operator (--) can also be embedded in a longer statement. See your favorite C reference book, or this link: http://www.lysator.liu.se/c/bwk-tutor.html#increment

In general you will not need that operator. Just do something like this:

char col;

int column_num;

column_num = 8; // whatever you like; in this case H

col = 'A' + column_num - 1;  // note automatic type casting is going on here, since we are mixing int and char in our math

if (col < 'A' || col > 'Z') Breakpoint();  // check the range somehow - this works during debugging only

Good luck.

--Ian

Message 5 of 7
(6,082 Views)

for some reason when I try to increment my col it doesn't change the value.   I have the following in my code currently

Fmt(cell, "%s1", colcounter);

ExcelRpt_SetCellValue(worksheetHandle, cell, Enum_datatypeString, data);

colcounter = colcounter + 1;

off++;

 

off here is my incrementer, Everytime I press the button that this is associated with off increments and a new value is put into data to be written across the top row of the spreadsheet.  The idea is putting headers across, but this is just practice for now to prove I can do it.  But when I set my colcounter as a watch variable the value of colcounter always remains 65, which is A.  my declarations are

char colcounter = 'A'

int off = 1

I can't figure out why the value of colcounter never changes.  Thanks

Niet.

0 Kudos
Message 6 of 7
(5,965 Views)
Is your colcounter global or local to the callback function?  Be sure it isn't being created and initialized each time you enter your callback - would be the case with a local variable. The easiest (though not most elegant) approach is to declare it globally, and initialize it to 'A' when the program launches.
 
Alternatively you can ensure the local variable is declared "static" within your function ("static char colcounter;") so it will retain its value betwwen calls, but you still need to handle initalization.
 
Also, it is best to use %c for formatting a single char character.
 
Hope this helps.
Message 7 of 7
(5,960 Views)