1. If there are few drop-down arrays in Excel (about 0~20), you can export them in the following way:
/**
* Excel API
*/
@SuppressWarnings("resource")
HSSFWorkbook book = new HSSFWorkbook();
HSSFSheet sheet = book.createSheet("xxxx");
/**
* 初始化参数
*/
Map<String, String> map = new HashMap<String, String>(); // 查询时用的map
List<Object> list = null;
String[] strs = null; // 用于下拉的数组
int startRow = 1; // 下拉的开始行
int endRow = 100; // 下拉的结束行
CellRangeAddressList regions = null;
DVConstraint constraint = null;
CellRangeAddressList addressList = null;
HSSFDataValidation validation = null; // 数据验证
map.put("namespace", "xxxxxxxxxx.xxxxxxxxxx"); // 查询数据
list = commonQueryService.queryList(map);
strs = StringUtil.mapListToStrs(list); // list转换为字符串数组
cellNum = SpuEnu.CATEGORY_1.getNumber(); // 下拉的列
regions = new CellRangeAddressList(startRow, endRow, cellNum, cellNum); // 开始行、结束行、开始列、结束列的下拉区域均被下拉替代
constraint = DVConstraint.createExplicitListConstraint(strs);
validation = new HSSFDataValidation(regions, constraint); // 绑定下拉框和作用区域
sheet.addValidationData(validation);
2. The problem is that if there are too many drop-down arrays, the following exception message will appear in POI:
String literals in formulas can't be bigger than 255 characters ASCII
The solution to this problem is not easy to find online, so I will post the solution below
Here’s the solution:
Please pay attention to these two places above: