Ahmed
Ahmed

Reputation: 11413

How to tell Range.NumberFormat to just put the data as is?


I'm doing some simple excel automation here, and somewhere around my code I have a switch block that checks for the CellType (custom enum I built) and based on that makes some formatting.
Here's my code:

            switch (celltype)
            {
                case CellType.String:
                    cellRangeFormat = cellrange.get_Range(cell, cell);
                    cellRangeFormat.NumberFormat = ????;
                    break;
                case CellType.Numeric:
                    cellRangeFormat = cellrange.get_Range(cell, cell);

                    cellRangeFormat.NumberFormat = ";0_);[Red](0)";
                    break;
                case CellType.Percentage:
                    cellRangeFormat = cellrange.get_Range(cell, cell);
                    cellRangeFormat.NumberFormat = "#,0.00 %";
                    break;
                case CellType.Decimal:
                    cellRangeFormat = cellrange.get_Range(cell, cell);
                    //cellrangeformat.NumberFormat = "#,0.00";
                    cellRangeFormat.NumberFormat = "#,##0.00_);[Red](#,##0.00)";
                    break;
                default:
                    break;
            }


Now the problem is that, in some columns the value is "000", and though I specify the CellType as string, excel would format the value and display the cell empty. If I select the cell, I see the value 0 in it. I want excel to stop formatting my values if they were string and just paste them as is.
How can I do that?

Upvotes: 1

Views: 2967

Answers (1)

Random832
Random832

Reputation: 39050

"@". You may need to set it before setting the value.

This also sets it up to really think of it as a string (so you can't do arithmetic on the cell in formulas, it will be left-aligned, etc).

Upvotes: 5

Related Questions