Excel Range apply new NumberFormat

538 Views Asked by At

I have an Microsoft.Office.Interop.Excel.Range which I fill with Data and set a new NumberFormat, like that:

wkbActive = (Excel.Workbook)Globals.ThisAddIn.Application.ActiveWorkbook;
wksActive = (Excel.Worksheet)wkbActive.ActiveSheet;
Excel.Range rngExample = wksActive.get_Range("A1");
rngExample.Value = "08.06.2015";
rngExample.NumberFormat = "ddd, d. M";
example.NumberFormat = "ddd, d. M";

The problem is that the new NumberFormat isn´t apply until I click into the cell. Is there an "update" Method or sth for the Range?!

1

There are 1 best solutions below

2
On

This is because you're setting the cell value to a string and this is not evaluated as a date until you click into the cell and press 'Enter'.

Excel stores dates as double which represents a time since 1900. You should set the Value2 property to a DateTime per this example:

rngExample.Value2 = new DateTime(2015, 6, 8);
rngExample.NumberFormat = "ddd, d. M";