0

I have a double value which I need to attach a percentage (%) sign to it like (80,00%). This gets written as a text in the Excel file due to my way of coding.

Here is the following code in C#.

var decimalValue = 80.0m;
(decimalValue / 100)?.ToString("#0.00%");

Output

80,00%

In Excel file I get a green issue mark which tells that the number is stored as text.

How to solve this issue buy storing the double value with the percentage as a 'number' so I won't get the green mark?

  • `%` is text. You want to save it as a number between 0 and 1, I suspect. You'll also need to set the right field type which won't be possible on a CSV, but is possible on an Excel file. – ProgrammingLlama Feb 06 '20 at 09:08
  • What file format are you using and are you using a library to write it? If it's CSV then you don't have a lot of control, other than writing the double out as a floating point value in the output. If xls or xlsx then what library are you using to write it? – Corey Feb 06 '20 at 09:10
  • or https://stackoverflow.com/questions/17281809/converting-excel-cell-to-percentage-using-epplus – mortb Feb 06 '20 at 09:11

1 Answers1

0

That worked for my poc project:

Excel c# convert cell to percentage

I found the right solution for my project when we use Aspose cells to make workbook and worksheets:

var templatePath = @("C:\Temp");
var workbook = new Workbook(templatePath);
var worksheet = workbook.Worksheets[0];
var decimalObject = 80.0m;
var convertedDecimalObject = (decimalObject / 100)?.ToString("#0.00%");
worksheet.Cells[0].PutValue(convertedDecimalObject, true);

The 'true' beside the converted decimal object tells that the object can be converted to other proper type in the excel file