特定のスタイルのセルを見つける

Microsoft Excelを使用する

MS Excelで特定のスタイルのセルを検索するために必要な手順は以下のとおりです。

  1. ホームタブ検索と選択を選択します。
  2. 検索を選択します。
  3. 高度なオプションが表示されていない場合はオプションを選択します。
  4. 書式ドロップダウンからセルから書式を選択を選択します。
  5. 検索したいスタイルのセルを選択します。
  6. すべてを見つけるをクリックします。選択したセルと似たスタイルを持つすべてのセルを見つけます。

Aspose.Cells for Java の使用

Aspose.Cells for Java は、ワークシート内の特定のスタイルでセルを検索する機能を提供します。このため、API は FindOptions.setStyle() プロパティを提供します。

サンプルコード

次のコードスニペットは、セル A1 と同じスタイルのすべてのセルを見つけ、それらのセル内のテキストを変更します。出力ファイルのスクリーンショットを見て、サンプルコードの出力を分析してください。

// 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(FindCellsWithSpecificStyle.class);
Workbook workbook = new Workbook(dataDir + "TestBook.xlsx");
Worksheet worksheet = workbook.getWorksheets().get(0);
// Access the style of cell A1
Style style = worksheet.getCells().get("A1").getStyle();
// Specify the style for searching
FindOptions options = new FindOptions();
options.setStyle(style);
Cell nextCell = null;
do {
// Find the cell that has a style of cell A1
nextCell = worksheet.getCells().find(null, nextCell, options);
if (nextCell == null)
break;
// Change the text of the cell
nextCell.putValue("Found");
} while (true);
workbook.save(dataDir + "out.xlsx");

コードの実行後、セル A1 と同じスタイルを持つすべてのセルにテキスト “Found” が追加されます。

スクリーンショット

todo:image_alt_text

図: スタイルを持つセルを含むソースファイル

以下のコードによって生成された出力ファイルです。セル A1 と同じスタイルを持つすべてのセルにテキスト “Found” が追加されています。

todo:image_alt_text

図: A1 スタイルで検索後に見つかったセルを含む出力ファイル