Search code examples
c#xlsnpoi

Format cell as Percentage with NPOI C#


I am trying to format a specific XLS cell to the "Percentage" format using NPOI. I am able to convert it to text using

(dataRow.GetCell(17) ?? dataRow.CreateCell(17)).CellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("Text");

But if I try do it with percentage it doesnt work. This is the code I am trying

(dataRow.GetCell(17) ?? dataRow.CreateCell(17)).CellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("Percentage");

It will just set that cell as Generic. How would I go about setting it as percentage? Please keep in mind I want the value e.g 0.05 to be in the cell (not 5%), but with the format as Percentage.


Solution

  • I found the answer. I had to use:

    (dataRow.GetCell(17) ?? dataRow.CreateCell(17)).CellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00%");
    

    It make it 2dp percentage without the symbol