Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format Accounting number format for cells using ClosedXML

Tags:

c#

closedxml

How to format a cell as Number Accounting category using ClosedXML?

Accounting format

like image 353
user1517433 Avatar asked Nov 15 '25 05:11

user1517433


1 Answers

The number format is:

_ * # ##0.00_ ;_ * -# ##0.00_ ;_ * "-"??_ ;_ @_

And set the NumberFormatId to 43.

You can get the number format by using Excel. Style the cell as you want it and then go to Custom number format. The number format will display there.

In ClosedXML, you can use it like this (be careful to escape the quotation marks):

using (var wb = new XLWorkbook())
{
    var ws = wb.AddWorksheet("Sheet1");
    var cell = ws.FirstCell();

    cell.Value = 0.0;
    cell.DataType = XLDataType.Number;
    cell.Style.NumberFormat.Format = "_ * # ##0.00_ ;_ * -# ##0.00_ ;_ * \"-\"??_ ;_ @_ ";
    cell.Style.NumberFormat.SetNumberFormatId(43);

    wb.SaveAs("test.xlsx");
}
like image 57
Francois Botha Avatar answered Nov 17 '25 19:11

Francois Botha