获取 Cell 带格式和不带格式的字符串值
Contents
[
Hide
]
Aspose.Cells提供方法Cell.GetStringValue()可用于获取带或不带任何格式的单元格的字符串值。假设您有一个值为 0.012345 的单元格,并且您已将其格式化为仅显示两位小数。然后它将在 Excel 中显示为 0.01。您可以使用 0.01 和 0.012345 检索字符串值Cell.GetStringValue()方法。它需要CellValueFormat策略枚举作为具有以下值的参数
- CellValueFormatStrategy.CellStyle
- CellValueFormatStrategy.DisplayStyle
- CellValueFormatStrategy.None
下面的示例代码解释了使用Cell.GetStringValue()方法。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Create workbook | |
Workbook workbook = new Workbook(); | |
// Access first worksheet | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Access cell A1 | |
Cell cell = worksheet.Cells["A1"]; | |
// Put value inside the cell | |
cell.PutValue(0.012345); | |
// Format the cell that it should display 0.01 instead of 0.012345 | |
Style style = cell.GetStyle(); | |
style.Number = 2; | |
cell.SetStyle(style); | |
// Get string value as Cell Style | |
string value = cell.GetStringValue(CellValueFormatStrategy.CellStyle); | |
Console.WriteLine(value); | |
// Get string value without any formatting | |
value = cell.GetStringValue(CellValueFormatStrategy.None); | |
Console.WriteLine(value); |