Converting Excel cell to percentage using epplus
Asked Answered
D

5

13

I would like to convert the value in to 2 decimal places. I am using EPPlus if the value is 66.6666667 and I would like to show it as 66.66% I tried the following code but its not working.

   foreach (var dc in dateColumns)
   {
       sheet.Cells[2, dc, rowCount + 1, dc].Style.Numberformat.Format = "###,##%";
   }

Please help.

Declared answered 24/6, 2013 at 17:52 Comment(0)
D
23

I found it!

I tried

 foreach (var dc in dateColumns)
  {
    sheet.Cells[2, dc, rowCount + 1, dc].Style.Numberformat.Format ="#0\\.00%";
   }
Declared answered 24/6, 2013 at 20:18 Comment(1)
it works when its only "0.00%". so without the '#' and without the '\'. Otherwise the value gets divided by hundred, such that the value stored and the value shown is not the same.Ceil
M
20

The correct formula is as follows:

 foreach (var dc in dateColumns)
  {
    sheet.Cells[2, dc, rowCount + 1, dc].Style.Numberformat.Format ="#0.00%";
  }

The Double slash in "#0\\.00%" leads to very unusual numbers when you try to expand the decimal places later

Marriott answered 16/5, 2017 at 2:25 Comment(1)
The only thing is if you already has the value as percentage, you need to divide it by 100 so that its displayed correctly when you open the sheet in excel.Harvell
H
3

As far as I checked format you set via epp is just a common Excel cell format.

In my case this was really helpful

Hillary answered 23/11, 2014 at 15:4 Comment(0)
S
0

If you want to do it on a cells range. You can use

using (ExcelRange Rng = wsDashboard.Cells["B6:J6"])
{
    Rng.Style.Numberformat.Format = "#0\\.00%";
}
Scheel answered 16/10, 2020 at 21:50 Comment(0)
A
0
sheet.Cells[1, 1, row, col].Style.Numberformat.Format = "##0.00\\%;[Red]-##0.00\\%";

It's worked for me.

Appolonia answered 1/3, 2023 at 15:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.