1

Im trying to format specific XLS cell to the "percentage" format. I read this post: Format cell as percantage NPOI c# When I use this solution it helps but the problem is that for example: 51.35% keep at excel as 5135.00%.

This is my code:

            XSSFCellStyle percentageStyle = (XSSFCellStyle)workbook.CreateCellStyle();
            percentageStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00%");

            XSSFCell c = (XSSFCell)dataRow.CreateCell(column.Ordinal);
            string val = row[column].ToString();

            if (!String.IsNullOrEmpty(val) && val.Contains('%'))
                        {
                            string val2 = val.Trim('%');
                            //Console.WriteLine("percent!" + val);
                            c.SetCellValue(Double.Parse(val2));
                            c.CellStyle = percentageStyle;




                        }

Is there any way to change it? I would like that the 'Number' value will be kept with '%'. Images are attached.Format cells number Format cells percentage

Coral Adar
  • 31
  • 4
  • In Excel, when a number is displayed as `51.35%`, then the value in the cell is `0.5135`. The solution to your problem is to divide your value by 100 – Flydog57 Jun 10 '20 at 06:21

1 Answers1

1

In Excel, when a number is displayed as 51.35%, then the value in the cell is 0.5135. The solution to your problem is to divide your value by 100

Coral Adar
  • 31
  • 4