Cell Data Types
- 2 minutes to read
- directly, by setting the Value property:
cell.Value = 10;
- as the result of a formula (Formula):
cell.Formula = "= SUM(3,7)";
The Spreadsheet determines the type of the inserted/calculated data and sets the cell's Type property value. Use the appropriate TypeValue property to get the cell value.
The available cell value types are listed below.
Identify the Type
You can format Numeric and DateTime cell values. For instance, a number can appear in a cell as a percentage, decimal, currency, accounting, date or time value.
Assign a format pattern to the Cell.NumberFormat property to format a value in a cell. For more information, please review the following topic: How to: Specify Number or Date Format for Cell Content.
Use the Cell.DisplayText property to get the formatted value as it is displayed in a cell.
worksheet.Cells["A1"].Formula = "= Now()"; worksheet.Cells["A1"].NumberFormat = "m/d/yy h:mm"; // worksheet.Cells["A1"].DisplayText returns "5/20/2020 13:08" worksheet.Cells["B1"].Value = 1.6; worksheet.Cells["B1"].NumberFormat = "0.0%"; // worksheet.Cells["B1"].DisplayText returns "160.0%"
Some settings, such as the group separator or decimal separator, depends on the culture used in your application. If you need to create culture-independent settings, apply recommendations from the following KB article to adjust them: How to create culture-independent settings in a project.