Cerca e sostituisci i dati in un intervallo
Contents
[
Hide
]
A volte, è necessario cercare e sostituire dati specifici in un intervallo, ignorando qualsiasi valore di cella al di fuori dell’intervallo desiderato. Aspose.Cells consente di limitare la ricerca ad un intervallo specifico. Questo articolo spiega come.
Aspose.Cells fornisce ilFindOptions.setRange() metodo per specificare un intervallo durante la ricerca di dati.
Supponiamo di voler cercare la stringa**“ricerca”** e sostituirlo con**“sostituire”** nell’intervallo**E3:H6**. Nello screenshot qui sotto, la stringa “cerca” è visibile in più celle ma vogliamo sostituirla solo in un determinato intervallo, qui evidenziato in giallo.
File di input
Dopo l’esecuzione del codice, il file di output è simile al seguente. Tutte le stringhe “search” all’interno dell’intervallo sono state sostituite con “replace”.
File di uscita
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(SearchReplaceDataInRange.class); | |
Workbook workbook = new Workbook(dataDir + "input.xlsx"); | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Specify the range where you want to search | |
// Here the range is E3:H6 | |
CellArea area = CellArea.createCellArea("E3", "H6"); | |
// Specify Find options | |
FindOptions opts = new FindOptions(); | |
opts.setLookInType(LookInType.VALUES); | |
opts.setLookAtType(LookAtType.ENTIRE_CONTENT); | |
opts.setRange(area); | |
Cell cell = null; | |
do { | |
// Search the cell with value search within range | |
cell = worksheet.getCells().find("search", cell, opts); | |
// If no such cell found, then break the loop | |
if (cell == null) | |
break; | |
// Replace the cell with value replace | |
cell.putValue("replace"); | |
} while (true); | |
// Save the workbook | |
workbook.save(dataDir + "output.xlsx"); |