1.导入POI依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.1</version>
</dependency>
2.代码实现
public void test(){
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("sheet0");
CellRangeAddressList cellRangeAddressList = new CellRangeAddressList(0, 0, 0, 0);
DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper();
DataValidationConstraint constraint = dataValidationHelper.createExplicitListConstraint(new String[]{"春", "夏", "秋", "冬"});
DataValidation validation = dataValidationHelper.createValidation(constraint, cellRangeAddressList);
sheet.addValidationData(validation);
wb.write(new FileOutputStream("D:\\test.xlsx"));
}