Converti dati numerici di testo in numeri
Contents
[
Hide
]
A volte, vuoi convertire i dati numerici inseriti come testo in numeri. È possibile inserire numeri come testo in Microsoft Excel inserendo un apostrofo prima di un numero, ad esempio**‘12345**. Excel quindi tratta il numero come una stringa. Aspose.Cells consente di convertire le stringhe in numeri.
Aspose.Cells for Java API fornisce ilCells.convertStringToNumericValue() che può essere utilizzato per convertire tutti i dati numerici stringa o testo in numeri.
Lo screenshot seguente mostra i numeri di stringa nelle celleA1:A17. I numeri delle stringhe sono allineati a sinistra.
File di input: numeri inseriti come stringhe di testo
Questi numeri di stringa sono stati convertiti in numeri utilizzandoCells.convertStringToNumericValue() nello screenshot seguente. Come puoi vedere, ora sono allineati a destra.
File di output: le stringhe sono state convertite in numeri
Il seguente codice di esempio illustra come convertire tutti i dati numerici stringa in numeri effettivi in tutti i fogli di lavoro.
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-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getDataDir(ConvertTextNumericDatatoNumber.class); | |
Workbook workbook = new Workbook(dataDir + "source.xlsx"); | |
for (int i = 0; i < workbook.getWorksheets().getCount(); i++) { | |
workbook.getWorksheets().get(i).getCells().convertStringToNumericValue(); | |
} | |
workbook.save(dataDir + "output.xlsx"); |