I want to apply the % (percentage) number format using open XML C#
I have numeric value 3.6 that I want to display that number in excel as `3.6%.
How do I a
Excel contains predefined formats to format strings in various ways. The s attribute on a cell element will refer to a style which will refer to a number format that will correspond to the percent format you want. See this question/answer for more information.
Here is the CellFormat object you will need to create in order to have the 0.00% mask applied to your number. In this case you want the predefined format number 10 or 0.00%:
CellFormat cellFormat1 = new CellFormat(){ NumberFormatId = (UInt32Value)10U, FontId = (UInt32Value)0U, FillId = (UInt32Value)0U, BorderId = (UInt32Value)0U, FormatId = (UInt32Value)0U, ApplyNumberFormat = true };
Here is a quick way to insert the CellFormat into the workbook:
CellFormats cellFormats = workbookPart.WorkbookStylesPart.Stylesheet.Elements().First();
cellFormats.Append(cellFormat);
uint styleIndex = (uint)cellFormats.Count++;
You will then need to get the cell that has the 3.6 in it and set it's s attribute (StyleIndex) to the newly inserted cell format:
Cell cell = workSheetPart.Worksheet.Descendants| ().SingleOrDefault(c => cellAddress.Equals("A1"));
cell.StyleIndex = styleIndex;
|