Skip to main content

XlFormatting.NetFormatString Property

Gets or sets the .NET-style format string that specifies how a numeric value should be displayed in a cell.

Namespace: DevExpress.Export.Xl

Assembly: DevExpress.Printing.v23.2.Core.dll

NuGet Package: DevExpress.Printing.Core

Declaration

public string NetFormatString { get; set; }

Property Value

Type Description
String

A String value that represents the format pattern.

Remarks

Use the NetFormatString property of the XlCellFormatting object to specify the .NET numeric format for a cell value. You can use both standard and custom format strings. Sometimes however, you may need to indicate whether the format string you specify is a data and time format string. For example, the "d" format specifier can be interpreted as the decimal format specifier or as the short date format specifier. To avoid ambiguity and to distinguish between the standard number and date and time format specifiers, use the XlFormatting.IsDateTimeFormatString property.

You can also use the static XlCellFormatting.FromNetFormat method to specify the .NET number format for a cell in a single step.

To apply number formatting options to a cell, pass the XlCellFormatting object to the IXlCell.ApplyFormatting method as a parameter, or assign it to the IXlCell.Formatting property.

Note that the specified .NET number format will be implicitly converted to the native Excel format. To directly specify the native Excel number format for a numeric value in a cell, use the XlFormatting.NumberFormat property. For details on how to apply different number formats to worksheet cells, refer to the How to: Specify Number Format for Cell Content example.

Example

Note

A complete sample project is available at https://github.com/DevExpress-Examples/excel-export-api-examples

// Create the header row for the ".NET number formats" category.
using (IXlRow row = sheet.CreateRow()) {
    using(IXlCell cell = row.CreateCell()) {
        // Set the cell value.
        cell.Value = ".NET number formats";
        // Apply the "Heading 4" predefined formatting to the cell.
        cell.Formatting = XlCellFormatting.Heading4;
    }
}
// Use the standard .NET-style format strings to display data in cells.
using (IXlRow row = sheet.CreateRow()) {
    using(IXlCell cell = row.CreateCell()) {
        cell.Value = "Standard formats:";
    }
    using(IXlCell cell = row.CreateCell()) {
        // Display 123.45 as 123.
        cell.Value = 123.45;
        cell.Formatting = XlCellFormatting.FromNetFormat("D", false);
    }
    using(IXlCell cell = row.CreateCell()) {
        // Display 12345 as 1.234500E+004.
        cell.Value = 12345;
        cell.Formatting = XlCellFormatting.FromNetFormat("E", false);
    }
    using(IXlCell cell = row.CreateCell()) {
        // Display 0.33 as 33.00%.
        cell.Value = 0.33;
        cell.Formatting = XlCellFormatting.FromNetFormat("P", false);
    }
    using(IXlCell cell = row.CreateCell()) {
        // Display the current date using the short date pattern.
        cell.Value = DateTime.Now;
        cell.Formatting = XlCellFormatting.FromNetFormat("d", true);
    }
    using(IXlCell cell = row.CreateCell()) {
        // Display the current time using the short time pattern.
        cell.Value = DateTime.Now;
        cell.Formatting = XlCellFormatting.FromNetFormat("t", true);
    }
}
// Use custom format strings to display data in cells.
using (IXlRow row = sheet.CreateRow()) {
    using(IXlCell cell = row.CreateCell()) {
        cell.Value = "Custom formats:";
    }
    using(IXlCell cell = row.CreateCell()) {
        // Display 123.456 as 123.46. 
        cell.Value = 123.45;
        cell.Formatting = XlCellFormatting.FromNetFormat("#0.00", false);
    }
    using(IXlCell cell = row.CreateCell()) {
        // Display 12345 as 1.235E+04.
        cell.Value = 12345;
        cell.Formatting = XlCellFormatting.FromNetFormat("0.0##e+00", false);
    }
    using(IXlCell cell = row.CreateCell()) {
        // Display 0.333 as Max=33.3%.
        cell.Value = 0.333;
        cell.Formatting = XlCellFormatting.FromNetFormat("Max={0:#.0%}", false);
    }
    using(IXlCell cell = row.CreateCell()) {
        // Apply the custom format string to the current date. 
        // Display days as 01–31, months as 01-12 and years as a four-digit number. 
        cell.Value = DateTime.Now;
        cell.Formatting = XlCellFormatting.FromNetFormat("dd-MM-yyyy", true);
    }
    using(IXlCell cell = row.CreateCell()) {
        // Apply the custom format string to the current time.
        // Display hours as 01-12, minutes as 00-59, and add the AM/PM designator. 
        cell.Value = DateTime.Now;
        cell.Formatting = XlCellFormatting.FromNetFormat("hh:mm tt", true);
    }
}
See Also