Corey Ogburn
Corey Ogburn

Reputation: 24739

Excel formatting my dates when I don't want it to?

I'm using excel interop to build reports. I have the string "April 8, 2013" as a string and I'm assigning it to a cell as such (where oSheet is an Excel._Worksheet and it's VB.Net):

oSheet.Cells(5, 2) = dateStr;

Nothing is modifying the NumberFormat or any other properties for that particular cell but what is being displayed when I open the output xlsx file is "8-Apr-13".

How can I force excel to just display the string without any additional formatting that might change the text?

Upvotes: 1

Views: 629

Answers (1)

John Willemse
John Willemse

Reputation: 6698

To force Excel to treat a date, or any non-textual data, as text, and thus not changing it's format automatically, you can use either of the following two methods:

  • Precede the date with a single quote, e.g. '08/01/2013
  • Set the NumberFormat of the range to Text with range.NumberFormat = "@";

Upvotes: 1

Related Questions