Java を使用して Excel でデータ検証を作成する方法

この簡単なチュートリアルでは、Java を使用して Excel でデータ検証を作成する方法について説明します。環境を確立するためのリソース、タスクを完了するために従うべき手順のリスト、Java を使用して Excel にデータ検証を挿入するための実行可能なサンプル コードを提供します。コードの説明が表示され、最後に検証ルールを含む XLS または XLSX ファイルを作成します。

Java を使用して Excel でデータ入力規則を作成する手順

  1. 検証を挿入する Aspose.Cells for Java を追加する環境を確立します
  2. 新しい workbook を作成し、それに参照データを追加するための worksheet を追加します
  3. 範囲クラス オブジェクトをインスタンス化し、その名前と目的の値のリストを設定します
  4. ターゲット シートの検証コレクションに新しい検証を作成し、そのセル領域を設定します
  5. 検証のその他のプロパティを設定する
  6. 検証が含まれているディスクにワークブックを保存します

これらの手順では、Java を使用して Excel でデータ検証を追加する方法 のプロセスについて説明します。ワークブックを作成し、その中にワークシートを作成してリスト データを追加し、範囲クラス オブジェクトをインスタンス化し、新しく作成した検証に設定する必要があります。最後に、検証のさまざまなプロパティを設定し、結果のワークブックをディスクに保存します。

Java を使用して Excel でデータ検証リストを作成するコード

import com.aspose.cells.CellArea;
import com.aspose.cells.License;
import com.aspose.cells.OperatorType;
import com.aspose.cells.Range;
import com.aspose.cells.Validation;
import com.aspose.cells.ValidationAlertType;
import com.aspose.cells.ValidationCollection;
import com.aspose.cells.ValidationType;
import com.aspose.cells.Workbook;
import com.aspose.cells.Worksheet;
public class Main {
public static void main(String[] args) throws Exception { // Main function to add validation to a worksheet
// Load a license
License lic = new License();
lic.setLicense("Aspose.Total.lic");
// Instantiate a workbook
Workbook workbook = new Workbook();
// Access the first sheet
Worksheet worksheet1 = workbook.getWorksheets().get(0);
// Create another sheet for reference data and get access to it
int i = workbook.getWorksheets().add();
Worksheet worksheet2 = workbook.getWorksheets().get(i);
// Create a range for the reference list
Range referenceRange = worksheet2.getCells().createRange("E1", "E4");
// Set the name property of the above-created range
referenceRange.setName("ReferenceRange");
// Fill the reference list to be used for validation
referenceRange.get(0,0).putValue("Tiny");
referenceRange.get(1, 0).putValue("Small");
referenceRange.get(2, 0).putValue("Medium");
referenceRange.get(3, 0).putValue("Large");
// Get a reference to the validations collection on the first sheet
ValidationCollection validations = worksheet1.getValidations();
// Create a cell Area where validation is to be implemented
CellArea area = new CellArea();
area.StartRow = 0;
area.EndRow = 4;
area.StartColumn = 0;
area.EndColumn = 0;
// Create a new validation for the given cell area defined above
validations.add(area);
Validation validation = validations.get(validations.add(area));
// Set type of validation
validation.setType (ValidationType.LIST);
// Set the type of operator
validation.setOperator(OperatorType.NONE);
// Set flag for in-cell drop-down
validation.setInCellDropDown(true);
// Set the formula by providing reference data range name
validation.setFormula1("=ReferenceRange");
// Enable the flag to show an error
validation.setShowError(true);
// Set the type of alert on error
validation.setAlertStyle(ValidationAlertType.STOP);
// Set the title
validation.setErrorTitle("Error Title");
// Set the message to be shown when an error is raised
validation.setErrorMessage("Please select data from the list");
// Save the output file
workbook.save("output.xls");
System.out.println("Done!");
}
}

このコードは、Java を使用して Excel にデータ検証を挿入する方法 を示しています。 Range クラス オブジェクトを使用して、検証オブジェクトが使用する範囲名と値のセットを設定します。各ワークシートには、新しい検証がプロパティおよび有効なセル領域と共に追加された検証のコレクションが含まれています。

このチュートリアルでは、Java を使用して Excel に検証を挿入する方法 について説明しました。 Excel でフィルターを適用するプロセスを知りたい場合は、Javaを使用してExcelでフィルターを適用する方法 の記事を参照してください。

 日本語