获取带格式和不带格式的单元格字符串值
Contents
[
Hide
]
Aspose.Cells提供一个方法Cell.GetStringValue(),它可用于获取单元格的字符串值,带有或不带有任何格式。假设您有一个单元格,其值为0.012345,并对其进行格式化以仅显示两位小数。那么在Excel中,它将显示为0.01。您可以使用Cell.GetStringValue()方法检索字符串值,它会显示为0.01或者0.012345。它接受CellValueFormatStrategy 枚举作为参数,该枚举具有以下值。
- CellValueFormatStrategy.CellStyle
- CellValueFormatStrategy.DisplayStyle
- CellValueFormatStrategy.DisplayString
- 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); |