5

Can someone give me some code to set the cell of an excel spreadsheet to a number format that use a max of 2 decimal places. Or would it work better it I change the data I am putting into the cell to a proper format? Here is a sample of data that is going in. Right now its going in as a string... col("ADJ").ToString() where col is a DataTable oject.

Nick LaMarca
  • 8,076
  • 31
  • 93
  • 152

3 Answers3

7

I'm not sure about VB, but in C# it would be:

worksheet.Cells["C3"].NumberFormat = "0.00"

or

worksheet.Cells["A:Z"].NumberFormat = "0.00"

or

Range.NumberFormat = "0.00"
Igby Largeman
  • 16,495
  • 3
  • 60
  • 86
4

Here's one way:

Selection.NumberFormat = """R"" #,##0.00;""R"" -#,##0.00"

Another way:

worksheet.Cells(x,y).NumberFormat = """R"" #,##0.00;""R"" -#,##0.00"

NOTE: this may vary with different versions of Excel.

RBarryYoung
  • 55,398
  • 14
  • 96
  • 137
  • I am using the worksheet.Cells(x,y) object can you give me an example with that I do not see it in my intellisense. – Nick LaMarca Apr 08 '10 at 20:25
0

In vb.Net:

worksheet.cellRange("X").NumberFormat="#,##0.00"

Sir-me
  • 1