-
Notifications
You must be signed in to change notification settings - Fork 287
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.
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:
ExcelPackageSettings.CultureSpecificBuildInNumberFormats.Add("de-DE",
new Dictionary<int, string>()
{
{14, "dd.mm.yyyy"},
{15, "dd. mmm yy"},
{16, "dd. mmm"},
{17, "mmm yy"},
{18, "hh:mm AM/PM" },
{22, "dd.mm.yyyy hh:mm"},
{39, "#,##0.00;-#,##0.00"},
{47, "mm:ss,f"}
});
To override EPPlus behaviour for other number formats you can also use the ExcelWorkbook.NumberFormatToTextHandler
callback function.
Using the ExcelWorkbook.NumberFormatToTextHandler
property you can add a callback function used to format the Text
property yourself.
package.Workbook.NumberFormatToTextHandler = TextHandler; //Set the new call back function to our own TextHandler.
The signature for this method looks like this: string TextHandler(NumberFormatToTextArgs options)
.
Here is a simple example:
public string TextHandler(NumberFormatToTextArgs options)
{
switch (options.NumberFormat.NumFmtId)
{
case 15:
if (options.Value is DateTime dt)
{
return dt.ToString("dd. mmm yy"); //Return your own formatted text.
}
break;
}
return options.Text;
}
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 |
EPPlus Software AB - https://epplussoftware.com
- What is new in EPPlus 5+
- Breaking Changes in EPPlus 5
- Breaking Changes in EPPlus 6
- Breaking Changes in EPPlus 7
- Addressing a worksheet
- Dimension/Used range
- Copying ranges/sheets
- Insert/Delete
- Filling ranges
- Sorting ranges
- Taking and skipping columns/rows
- Data validation
- Comments
- Freeze and Split Panes
- Header and Footer
- Autofit columns
- Grouping and Ungrouping Rows and Columns
- Formatting and styling
- Conditional formatting
- Using Themes
- Working with custom named table- or slicer- styles