tags:

views:

26

answers:

1

Hi guys,

I am currently exporting data from an excel import using excel interop. I've been fiddling with this for a few hours now, but can't get the export as I desire.

I'm taking a range of 2 columns and about 1k rows from an excel doc, and simply want to add a comma to the end of each column, but because of the way that I'm looping through the range values, I can't seem to get my head around it. Anyway, here's the code:

for (long colCounter = 1; colCounter <= iCols; colCounter++)
{
    for (long rowCounter = 1; rowCounter <= iRows; rowCounter++)
    {
        //Write the next value into the string.
        valueString = 
            valueString.ConcatImport(Convert.ToString(saRet[rowCounter, colCounter]), "\n");
     }
}

I think I need to add another loop somewhere to check if I'm at a new column or not, and then add the comma value on. Similar to how its implemented for new lines in each row, but I just can't seem to get there!

Thanks for any help!

+3  A: 
StringBuilder sb=new StringBuilder(); 
for (long rowCounter = 1; rowCounter <= iRows; rowCounter++)
 {
    for (long colCounter = 1; colCounter <= iCols; colCounter++)
    {
        sb.Append(Convert.ToString(saRet[rowCounter, colCounter]));
        sb.Append(",");       
     }
sb=sb.ToString().TrimEnd(',');
sb.Append("\n");
}
AsifQadri
Thank you! Worst thing is, is that I'd actually implemented this earlier, but after staring at it for so long I hadn't realised it had worked so spent an extra 3 hours banging my head against a wall! Haha!
Andy