The ExcelRange.Text property
The ExcelRange.Text method returns the cells formatted value as a string.
In most cases EPPlus converts the Value property to the same Text value as your spreadsheet application, but some number formats are converted to a localized format that differs between .NET and the spread sheet application.
To get the correct format in all cases you can add your own handling for this.
Using the ExcelPackageSettings.CultureSpecificBuildInNumberFormats¶
Some of the build in number formats are formatted differently depending on the regional setting. By default EPPlus uses the standard OOXML number formats as specified in ECMA-376, section 18.8.30. To support regional formats you can add your own formats like this:
To override EPPlus behaviour for other number formats you can also use the ExcelWorkbook.NumberFormatToTextHandler callback function.
ExcelWorkbook.NumberFormatToTextHandler¶
Using the ExcelWorkbook.NumberFormatToTextHandler property you can add a callback function used to format the Text property yourself.
The signature for this method looks like this: string TextHandler(NumberFormatToTextArgs options).
Here is a simple example:
Public Function TextHandler(options As NumberFormatToTextArgs) As String
Select Case options.NumberFormat.NumFmtId
Case 15
If TypeOf options.Value Is DateTime Then
Dim dt = CType(options.Value, DateTime)
Return dt.ToString("dd. mmm yy") 'Return your own formatted text.
End If
End Select
Return options.Text
End Function
The NumberFormatToTextArgs class¶
This class returns information about the cell to be formatted.
| Property | Data type | Description |
|---|---|---|
| Worksheet | ExcelWorksheet | The worksheet of the cell to be formatted |
| Row | int | The row of cell to be formatted |
| Column | int | The column of the cell to be formatted |
| NumberFormat | ExcelNumberFormatXml | The number format settings for the cell to be formatted |
| Value | object | The value to be formatted |
| Text | string | The text string formatted by EPPlus |
See also¶
- Formatting and Styling (Contains Number Format syntax)