如何使用 Java 在 Excel 中创建数据验证

这个简短的教程将教您如何使用 Java 在 Excel 中创建数据验证。它提供了用于建立环境的资源、完成任务要遵循的步骤列表,以及用于使用 Java 在 Excel 中插入数据验证 的可运行示例代码。您将获得代码的描述,最后创建一个包含验证规则的 XLSXLSX 文件。

使用 Java 在 Excel 中创建数据验证规则的步骤

  1. 搭建环境添加Aspose.Cells for Java插入验证
  2. 创建一个新的 workbook 并添加一个 worksheet 以向其添加参考数据
  3. 实例化范围类对象并设置其名称和所需的值列表
  4. 在target sheet的validations集合中新建一个validation,并设置其cell area
  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 中应用过滤器 上的文章。

 简体中文