Suchen und Ersetzen von Daten in einem Bereich

Aspose.Cells bietet dieFindOptions.setRange() Methode zur Angabe eines Bereichs bei der Suche nach Daten.

Angenommen, Sie möchten nach der Zeichenfolge suchen**“Suche”** und ersetzen Sie es durch**“ersetzen”** im Sortiment**E3:H6**. Im Screenshot unten ist die Zeichenfolge „search“ in mehreren Zellen zu sehen, aber wir wollen sie nur in einem bestimmten Bereich ersetzen, hier gelb hervorgehoben.

Eingabedatei

todo: Bild_alt_Text

Nach der Ausführung des Codes sieht die Ausgabedatei wie folgt aus. Alle „Such“-Strings innerhalb des Bereichs wurden durch „Ersetzen“ ersetzt.

Ausgabedatei

todo: Bild_alt_Text

// 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");

Zum Thema passende Artikel