简体   繁体   中英

How to keep numeric value of Percentage cell NPOI in C#

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM