---
title: "POI导出Excel，在Excel里生成下拉框"
author: "907182878"
date: "2014-05-04T13:49:22Z"
tags: []
categories: ["企业架构"]
slug: "poi-excel-excel"
draft: false
lastmod: "2014-05-04T13:49:22Z"
id: 2059624
url: "https://micwind.iteye.com/blog/2059624"
---

```
	public static void main(String[] args) {
		ExportToExcel export = new ExportToExcel();
		HSSFSheet sheet = export.createSheet("工作表1");
		//第3列从第二行开始的单元格
		sheet.addValidationData(ExportToExcel.setValidate(1, 2, new String[] {
				"深圳", "广州" }));
	}

	public static HSSFDataValidation setValidate(int firstRow, int firstCol,
			String[] explicitListValues) {
		// 创建一个规则
		// DVConstraint.create
		DVConstraint constraint1 = DVConstraint
				.createExplicitListConstraint(explicitListValues);

		// 设定在哪个单元格生效
		int lastRow = 1000;// 一千行都有效
		int lastCol = firstCol;
		CellRangeAddressList regions = new CellRangeAddressList();
		regions.addCellRangeAddress(firstRow, firstCol, lastRow, lastCol);

		// 创建规则对象
		HSSFDataValidation data_validation = new HSSFDataValidation(regions,
				constraint1);
		data_validation.setEmptyCellAllowed(false);
		data_validation.setShowPromptBox(false);
		// data_validation.createErrorBox("无效输入!", "请选择下拉列表");
		// data_validation.createPromptBox("输入提示!", "请选择下拉列表里的内容!");
		return data_validation;
	}
```