Show a cell as Excel shows it
A cell holds a value and a number format. What you see in Excel is the value through the format: 1234.5 under #,##0.00 is 1,234.50. DisplayText gives that text for a cell, and XlsxNumberFormatter gives it for any value and any code.
dotnet add package DocWright.Spreadsheet
What a cell shows
using DocWright.Spreadsheet;
XlsxDocument book = XlsxDocument.Create();
XlsxDocumentSheet sheet = book.Sheets[0];
sheet.SetValue("A1", XlsxCellValue.Number(-1234.5m));
sheet.SetStyle("A1", book.DefaultStyle.With(numberFormat: "#,##0.00;[Red](#,##0.00)"));
sheet.SetValue("A2", XlsxCellValue.Date(new DateTime(2026, 11, 17)));
sheet.SetStyle("A2", book.DefaultStyle.With(numberFormat: "dddd d mmmm yyyy"));
sheet.SetValue("A3", XlsxCellValue.Number(0.125m));
sheet.SetStyle("A3", book.DefaultStyle.With(numberFormat: "0.0%"));
sheet.SetValue("A4", XlsxCellValue.Number(0.333333333333333m)); // no format: the general format
foreach (XlsxDocumentCell cell in sheet.Cells)
{
string colour = cell.DisplayColor is null ? string.Empty : $" (the format names {cell.DisplayColor})";
Console.WriteLine($"{cell.Reference}: value {cell.Value.NumberValue}, shown '{cell.DisplayText}'{colour}");
}
It prints the first four lines below; the rest come from the samples further down this page.
A1: value -1234.5, shown '(1,234.50)' (the format names FF0000)
A2: value 46343, shown 'Tuesday 17 November 2026'
A3: value 0.125, shown '12.5%'
A4: value 0.333333333333333, shown '0.333333333'
1,234.50
1.01
2 3/4
1.23E+04
1.2M
17 Nov 2026 8:30 AM
36:00
nil
1.23457E+11
1.234,50
Dienstag, 17. November 2026
mardi 17 novembre 2026
1E+08 | ##### (overflow: True)
(1,234.50) | Tuesday 17 November 2026 | 12.5% | 0.333333333
| Member | Gives |
|---|---|
cell.DisplayText |
The text Excel shows: the value through the cell's number format, in the workbook's date system. Empty for an empty cell. |
cell.DisplayColor |
The colour the format names for this value ([Red] in the section that applies), or null. It is the format's colour, not the font's: cell.Style.FontColor has that. |
cell.Value, cell.NumberFormat |
The raw value and the format code, as before. |
Text shows as its text, a boolean as TRUE or FALSE, an error as its literal. A formula cell shows the result stored in the file, or the one Calculate gave it (Calculate formulas).
Any value, any code
using DocWright.Spreadsheet;
static string Show(decimal value, string code) => XlsxNumberFormatter.Format(XlsxCellValue.Number(value), code).Text;
Console.WriteLine(Show(1234.5m, "#,##0.00"));
Console.WriteLine(Show(1.005m, "0.00")); // rounded half away from zero, on the decimal
Console.WriteLine(Show(2.75m, "# ?/?"));
Console.WriteLine(Show(12345m, "0.00E+00"));
Console.WriteLine(Show(1234567m, "#,##0.0,,\"M\"")); // each trailing comma divides by a thousand
Console.WriteLine(Show(46343.3541666667m, "d mmm yyyy h:mm AM/PM"));
Console.WriteLine(Show(1.5m, "[h]:mm")); // a day and a half of elapsed hours
Console.WriteLine(Show(0m, "0.00;-0.00;\"nil\"")); // the third section is zero's
Console.WriteLine(Show(123456789012m, "General")); // eleven characters at most
XlsxNumberFormatter.Format(value, code, context) returns an XlsxFormattedText:
| Member | Meaning |
|---|---|
Text |
The text. |
Color |
The colour the section names, or null. |
IsFill |
The section has a * fill, a character Excel repeats until the cell is full. |
IsOverflow |
The value cannot be shown and Text is a run of #: a negative date in the 1900 system, a date past the year 9999, or a value wider than the column that was given. |
What a code can hold:
| In a code | Does |
|---|---|
0 # ? |
A digit: always, only when it counts, or a space in its place. |
. , |
The decimal point; a comma between digits groups thousands, a comma after the last digit divides by a thousand. |
% |
Multiplies by a hundred. |
E+00 E-0 |
Scientific notation. ##0.0E+0 steps the exponent in threes. |
# ?/? # ??/16 |
A fraction, with a denominator of that many digits or exactly that one. |
; |
Sections: positive; negative; zero; text. |
[>=1000] |
A condition that chooses the section. |
[Red] [Color5] |
A colour for the section. |
"text" \x _x *x |
Text, one character of text, a blank as wide as a character, a fill. |
@ |
The cell's text. |
yyyy mm mmm dddd h ss.00 AM/PM [h] |
Dates and times. m is minutes beside hours or seconds. |
[$-407] [$€-2] |
A locale for the names; a currency text. |
General |
The general format: at most eleven characters. |
Note
Measured against Excel, not read from a specification. The rules come from 8,372 pairs of a code and a value and the text Excel shows for each; 7,782 are reproduced to the character and the tests run every one. Rounding is half away from zero on the decimal digits of the value, to fifteen significant digits: 1.005 under 0.00 is 1.01.
A culture, and a column's width
using System.Globalization;
using DocWright.Spreadsheet;
var german = new XlsxFormatContext { Culture = new CultureInfo("de-DE") };
Console.WriteLine(XlsxNumberFormatter.Format(XlsxCellValue.Number(1234.5m), "#,##0.00", german).Text);
Console.WriteLine(XlsxNumberFormatter.Format(XlsxCellValue.Number(46343m), "dddd, d. mmmm yyyy", german).Text);
// A locale the code names itself decides the names, whatever the context.
Console.WriteLine(XlsxNumberFormatter.Format(XlsxCellValue.Number(46343m), "[$-40C]dddd d mmmm yyyy").Text);
// A column's width: what the general format keeps, and what is too wide.
var narrow = new XlsxFormatContext { ColumnWidthCharacters = 5m };
XlsxFormattedText general = XlsxNumberFormatter.Format(XlsxCellValue.Number(123456789m), "General", narrow);
XlsxFormattedText tooWide = XlsxNumberFormatter.Format(XlsxCellValue.Number(123456789m), "#,##0", narrow);
Console.WriteLine($"{general.Text} | {tooWide.Text} (overflow: {tooWide.IsOverflow})");
XlsxFormatContext |
Default | Meaning |
|---|---|---|
Culture |
null |
Month and day names, AM and PM, the decimal and group separators. null is the fixed default: English names, AM/PM, a point and a comma. It never reads the machine's culture. |
Uses1904DateSystem |
false |
The workbook counts dates from 1904. |
ColumnWidthCharacters |
null |
The column's width. Without it the text is the unconstrained form. |
DisplayText uses the default context. For a culture or a width, call XlsxNumberFormatter.Format(cell.Value, cell.NumberFormat, context).
Warning
Three things depend on the column's width, and two of them Excel measures in pixels. How many digits the general format keeps is reproduced to the character. How often a * fill is repeated and whether a value is too wide are counted here in characters; Excel measures the text in pixels of the cell's font, so -46343.00 fits its column of 8.43 characters there and is ######## here. Treat a fill's length and a borderline overflow as close, not exact.
Two more differences, both small:
A/PshowsAorPwith a 12-hour clock, as English (United States) Excel does. On a machine set to another English locale Excel showed those three characters as text.[$-F400]and[$-F800], the system's own long time and long date, are shown in their English (United States) forms so that the text is the same on every machine.
Export what is shown
using DocWright.Spreadsheet;
var asShown = new XlsxExportOptions { ValuesAs = XlsxExportValues.DisplayedText, FirstRowIsHeader = false };
object?[,] shown = sheet.ExportArray(null, asShown);
Console.WriteLine($"{shown[0, 0]} | {shown[1, 0]} | {shown[2, 0]} | {shown[3, 0]}");
With ValuesAs = XlsxExportValues.DisplayedText, an export gives the text each cell shows: every element of an array and every column of a new table is a string. In a table or an object of your own, a column or a property of type string gets the shown text and any other type is still given the value. An error value is then the text it shows and is not listed as an issue.