Rob
Rob

Reputation: 3

C# Excel Formatting Issue

I have an issue which I cannot solve. I'm writing some strings into Excel cells. But Excel is formatting them in a wrong way. These strings are IDs, for example 12A1, 12D1, 12E1 etc.. Everything works well except from the "12E1" string which is seen as an exponential. I tried with numberformat but it doesn't work.

For example the string "12E3" is written as 1.2e03, or 1200 and so on. i just want to see "12E3" written there. This program is going to be used by many PCs so i cannot change the Microsoft Excel General Settings for everyone!

Thank you and sorry for my poor english!

EDIT: Here's the code:

foreach (var Citem in ComQuery)
{
    i++;
    xlWorkSheet.Cells[i, 1] = "'" + Citem.commessaID; //the item is "12D3"
    xlWorkSheet.Cells[i, 2] = Citem.commessaID;       //the item is "12D3"
}

The first cell gives me the string "12D3" but with a warning on Excel, the second cell gives me 1.2E+004

Upvotes: 0

Views: 251

Answers (2)

shahkalpesh
shahkalpesh

Reputation: 33476

xlWorkSheet.Cells[i, 2].NumberFormat = "@";
xlWorkSheet.Cells[i, 2] = Citem.commessaID;

Upvotes: 1

Pat Mustard
Pat Mustard

Reputation: 1902

You could do something like this:

var id = "12E1"
ws.Range("A1").Value = "'" + id

which will give your cell the following value:

'12E1

Upvotes: 0

Related Questions