Ottieni il valore di una stringa della cella con o senza formattazione
Aspose.Cells fornisce un metodo Cell.getStringValue() che può essere usato per ottenere il valore stringa della cella con o senza formattazione. Supponi di avere una cella con valore 0.012345 e di averla formattata per mostrare solo due decimali. Verrà mostrata come 0.01 in Excel. È possibile recuperare valori stringa sia come 0.01 che come 0.012345 usando il metodo Cell.getStringValue(). Accetta come parametro l’enum CellValueFormatStrategy che ha i seguenti valori.
Ottieni il valore stringa della cella con e senza formattazione
Il seguente esempio di codice spiega l’uso del metodo Cell.getStringValue().
// 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); |
Output della console
Di seguito è riportato l’output della console del codice di esempio sopra.
0.01
0.012345