Gold
Gold

Reputation: 62524

How to make Excel with zero before the number in C#

I make from my program an excel file (xls or csv).

I send 00123 and in Excel I see 123

How I can send and see 00123

Thanks in advance

Upvotes: 1

Views: 1028

Answers (2)

A G
A G

Reputation: 22587

Its because excel is treating the data as 'numeric'. A simple way to force Excel to accept anything as text is to prepend an apostrophe to the text.

e.g. to write an integer 123 as 00000123 just write:

ActiveCell = "'" & Format(123, "00000000")

EDIT: Another solution is to set the Cells NumberFormatProperty to text:

Worksheet.GetRange(..).EntireColumn.NumberFormat = "@"

You might want to see this article: Excel Cell Auto Format

Upvotes: 3

Eric Fortis
Eric Fortis

Reputation: 17360

In C# to see the CSV with the padding use

myVar.PadLeft(5,'0')

In Excel set the number format to custom 00000 or ZipCode

Upvotes: 2

Related Questions