ワークシートに条件付き書式設定を適用する

条件付き書式を使用する

この記事は、以下のタスクを実行します:

  1. Aspose.Cellsを使用してセルの値に基づいて条件付き書式を適用する
  2. Aspose.Cellsを使用して数式に基づいて条件付き書式を適用する

タスク1: Aspose.Cellsを使用してセルの値に基づいて条件付き書式を適用する

  1. Aspose.Cells.zipをダウンロードしてインストールします
    1. ダウンロード Aspose.Cells for Java。
    2. 開発コンピュータにそれを解凍します。 全てのAsposeコンポーネントは、インストールされると評価モードで動作します。評価モードには時間制限はなく、生成されたドキュメントにウォーターマークが挿入されます。
  2. プロジェクトを作成します。 EclipseなどのJavaエディタを使用してプロジェクトを作成するか、テキストエディタを使用してシンプルなプログラムを作成します。
  3. クラスパスを追加します。 Eclipseを使用してクラスパスを設定するには、次の手順を実行します。
    1. Aspose.Cells.zipからAspose.Cells.jarとdom4j_1.6.1.jarを抽出します。
    2. Eclipseでプロジェクトのクラスパスを設定します。
      1. Eclipseでプロジェクトを選択し、ProjectメニューからPropertiesを選択します。
      2. ダイアログの左側で Java Build Path を選択します。
      3. Librariesタブで、Add JARs または Add External JARs を選択して、Aspose.Cells.jar と dom4j_1.6.1.jar を選択し、ビルドパスに追加します。
    3. AsposeのコンポーネントのAPIを呼び出すアプリケーションを作成します。 または、WindowsのDOSプロンプトで実行時にパスを設定することもできます。
  javac -classpath %classpath%;e:\Aspose.Cells.jar;  ClassName .javajava -classpath %classpath%;e:\Aspose.Cells.jar;  ClassName  
  1. セルの値に基づいて条件付き書式を適用します**。 以下は、コンポーネントがタスクを達成するために使用したコードです。それはセルに条件付き書式を適用します。
// 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(ConditionalFormattingOnCellValue.class);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
Worksheet sheet = workbook.getWorksheets().get(0);
// Adds an empty conditional formatting
int index = sheet.getConditionalFormattings().add();
FormatConditionCollection fcs = sheet.getConditionalFormattings().get(index);
// Sets the conditional format range.
CellArea ca = new CellArea();
ca.StartRow = 0;
ca.EndRow = 0;
ca.StartColumn = 0;
ca.EndColumn = 0;
fcs.addArea(ca);
// Sets condition formulas.
int conditionIndex = fcs.addCondition(FormatConditionType.CELL_VALUE, OperatorType.BETWEEN, "50", "100");
FormatCondition fc = fcs.get(conditionIndex);
fc.getStyle().setBackgroundColor(Color.getRed());
workbook.save(dataDir + "output.xls");

上記のコードを実行すると、出力ファイル(output.xls)の最初のワークシートのA1セルに条件付き書式が適用されます。A1のセル値に依存して条件付き書式が適用されます。A1のセル値が50から100の間である場合、条件付き書式により背景色が赤になります。生成されたXLSファイルのスクリーンショットを参照してください。

A1の値が50未満の出力Excelファイル

todo:image_alt_text

A1が50から100の間の出力Excelファイル

todo:image_alt_text

タスク2: Aspose.Cellsを使用して数式に基づいて条件付き書式を適用する

  1. 数式に応じて条件付き書式を適用します。 以下は、コンポーネントがタスクを達成するために使用した実際のコードです。それは「B3」に条件付き書式を適用します。
// 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(ConditionalFormattingBasedOnFormula.class);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
Worksheet sheet = workbook.getWorksheets().get(0);
ConditionalFormattingCollection cfs = sheet.getConditionalFormattings();
int index = cfs.add();
FormatConditionCollection fcs = cfs.get(index);
// Sets the conditional format range.
CellArea ca = new CellArea();
ca = new CellArea();
ca.StartRow = 2;
ca.EndRow = 2;
ca.StartColumn = 1;
ca.EndColumn = 1;
fcs.addArea(ca);
// Sets condition formulas.
int conditionIndex = fcs.addCondition(FormatConditionType.EXPRESSION, OperatorType.NONE, "", "");
FormatCondition fc = fcs.get(conditionIndex);
fc.setFormula1("=IF(SUM(B1:B2)>100,TRUE,FALSE)");
fc.getStyle().setBackgroundColor(Color.getRed());
sheet.getCells().get("B3").setFormula("=SUM(B1:B2)");
sheet.getCells().get("C4").setValue("If Sum of B1:B2 is greater than 100, B3 will have RED background");
workbook.save(dataDir + "output.xls");

上記のコードを実行すると、出力ファイル(output.xls)の最初のワークシートのB3セルに条件付き書式が適用されます。適用される条件付き書式は、B1とB2の合計を計算する数式に依存します。生成されたXLSファイルのスクリーンショットをご覧ください。

B3の値が100未満の出力Excelファイル

todo:image_alt_text

B3が100を超える出力Excelファイル

todo:image_alt_text

結論