获取带格式和不带格式的单元格字符串值
Contents
[
Hide
]
Aspose.Cells提供了Cell.getStringValue()方法,可用来获取单元格的字符串值,无论是否带格式。例如,假设你有一个值为0.012345的单元格,并将其格式化为只显示两位小数,那么在Excel中显示为0.01。你可以用Cell.getStringValue()方法同时以0.01和0.012345的形式获取字符串值。此方法接受CellValueFormatStrategy枚举作为参数,枚举值如下:
获取有格式和无格式的单元格字符串值
以下示例代码演示了Cell.getStringValue()方法的用法。
This file contains hidden or 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-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(GetCellStringValue.class); | |
// Create workbook | |
Workbook workbook = new Workbook(); | |
// Access first worksheet | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Access cell A1 | |
Cell cell = worksheet.getCells().get("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.setNumber(2); | |
cell.setStyle(style); | |
// Get string value as Cell Style | |
String value = cell.getStringValue(CellValueFormatStrategy.CELL_STYLE); | |
System.out.println(value); | |
// Get string value without any formatting | |
value = cell.getStringValue(CellValueFormatStrategy.NONE); | |
System.out.println(value); |
控制台输出
以下是上面示例代码的控制台输出。
0.01
0.012345