Shaikh Atik Ahmed
Shaikh Atik Ahmed

Reputation: 11

Epplus not able to format the column as number

I have assigned the data to a particular column in my code. Converted the columns as number

workSheet.Cells["B3:B11"].Style.Numberformat.Format = "#,##0.00";    
workSheet.Cells["B3"].Value = obj.Number1;
workSheet.Cells["B4"].Value = obj.Number2;
workSheet.Cells["B5"].Value = obj.Number3;
workSheet.Cells["B6"].Value = obj.Number4;
workSheet.Cells["B7"].Value = obj.Number5;
workSheet.Cells["B8"].Value = obj.Number6;
workSheet.Cells["B9"].Value = obj.Number7;
workSheet.Cells["B10"].Value = obj.Number8;
workSheet.Cells["B11"].Value = obj.Number9;

When excel is generated, the column are in text format. I want them to be in number format. Need help.

Upvotes: 0

Views: 9283

Answers (1)

Ernie S
Ernie S

Reputation: 14270

If those are strings then Epplus/excel will respect that and save them as strings so the formatting you set will be ignored. You will likely get the green triangles in the corners of the cells indication that they look like numbers.

To treat them as numbers, you need to convert them to doubles like this:

workSheet.Cells["B3"].Value = Double.Parse(obj.Number1);

If you are confident that they will never be null or improperly formatted then the above should be good enough. Otherwise, might want to do some checks like this:

double number;
if (obj.Number1 != null && Double.TryParse(obj.Number1, out number))
    workSheet.Cells["B3"].Value = number;

Upvotes: 1

Related Questions