1

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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Gold
  • 60,526
  • 100
  • 215
  • 315
  • 1
    I'm happy to see you're always thanking us in advance - nice and polite. Just as a note: when you do, please use the proper spelling of "Thanks in advance" - there's no apostrophe in that phrase...... – marc_s Dec 07 '10 at 06:37

2 Answers2

3

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

A G
  • 21,087
  • 11
  • 87
  • 112
2

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

Eric Fortis
  • 16,372
  • 6
  • 41
  • 62