views:

64

answers:

1

I am creating an Excel document using owc11. I am providing the dates in dd/mm/yyyy format. I am doing something like this in the code:

for (..looping through results..)
{
    Range c = EmailStats.putCellValue(sheet, row, 1, val);
    c.set_NumberFormat("dd/mm/yyyy");
}

private static Range putCellValue(Worksheet sheet, int row, int col, String val) 
{
    Range cell = (Range) sheet.Cells[row, col];
    cell.set_Value(XlRangeValueType.xlRangeValueDefault, val);
    return cell;
}

Now when for the val argument I set the date format as "dd/mm/yyyy" or not set it at all, the behaviour I get is mm/dd/yyyy from the 1st of a month up and till the 12th and then it swaps back to dd/mm/yyyy. So owc11 thinks that it knows better and swaps the days and month around (like in the US format) and when it is over the 12th date it sticks to the UK format.

Val is declared as String because it may not always be a date. It may be a date, a day, a user name, group name etc depending on how we group/sort our data. Also it may be a selection of days.

After experimenting a while I figured out that the only way to solve this is to use the universal date format of yyyy/mm/dd. However that may create other integration problems. So I was hoping that there may be a way to enforce the dd/mm/yyyy format, so please any suggestions are welcome.

A: 

Hello,

Try to set val as not string. Try assign val as DateTime.

cell.set_Value(XlRangeValueType.xlRangeValueDefault, val);
igor
Indeed that worked. If I pass the value as a DateTime and and then format the cell with the "dd/mm/yyyy" format it works fine. Many thanks.
Vasilis