範囲内のデータの検索と置換
Contents
[
Hide
]
場合によっては、目的の範囲外のセル値を無視して、範囲内の特定のデータを検索して置換する必要があります。 Aspose.Cells を使用すると、検索を特定の範囲に制限できます。この記事では、その方法について説明します。
Aspose.Cells はFindOptions.setRange() データ検索時の範囲指定方法。
文字列を検索するとします。“探す”そしてそれを“交換”範囲内E3:H6.以下のスクリーンショットでは、文字列「検索」が複数のセルに表示されていますが、ここでは黄色で強調表示されている特定の範囲でのみ置き換えたいと考えています。
入力ファイル
コードの実行後、出力ファイルは次のようになります。範囲内のすべての「検索」文字列は「置換」に置き換えられました。
出力ファイル
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(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"); |