原因是使用easypoi自带的下拉预选值无效,并且还带来了版本冲突的问题,所以只能自己来解决, easypoi的使用请参考: http://doc.wupaas.com/docs/easypoi/easypoi-1c0u9dt0lqnpl
这里是说明使用easypoi生成的Workbook之后如何给单元格设置下拉预选值,但是也有缺点,就是前提必须要知道设置下拉预选值单元格的范围,也就是需要指定范围,行列的范围,原理其实是创建sheet页,这是poi的写法,也就是一整个模板,代码如下:
// poi导出,生成Workbook Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("生活老师导入","Sheet0", ExcelType.XSSF), ExportDormDTO.class, list); // 设置下拉预选值 String[] dropDownValue = new String[]{"A", "B", "C", "D"}; String dropDownSheetName= "生活评价"; XSSFDataValidationHelper dropDownValidationHelper = createXSSFDataValidationHelper(workbook, dropDownSheetName, dropDownValue); DataValidationConstraint dropDOwnValidationConstraint = dropDownValidationHelper.createFormulaListConstraint(dropDownSheetName + "!$A$1:$A$" + dropDownValue.length); Sheet firstSheet = workbook.getSheet("Sheet0"); CellRangeAddressList drowDownValueCoveringRowsAndCloumns = new CellRangeAddressList(2, firstSheet.getLastRowNum(), 5, 9); XSSFDataValidation dropDownValidation =(XSSFDataValidation)dropDownValidationHelper.createValidation(dropDOwnValidationConstraint, drowDownValueCoveringRowsAndCloumns); firstSheet.addValidationData(dropDownValidation); downLoadExcel("生活老师评价导入.xlsx", response, workbook); ------------------------------------------------------------------------------- 下面是固定写法,是上面代码中使用到的局部方法 // 流导出 private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) throws IOException { response.reset(); response.setContentType("application/x-download"); response.setHeader("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes("utf-8"), "ISO-8859-1")); workbook.write(response.getOutputStream()); } // 生成预选值的sheet页 private static XSSFDataValidationHelper createXSSFDataValidationHelper(Workbook workbook, String name, String[] strList) { Sheet sheet = workbook.createSheet(name); // 循环往该sheet中设置添加下拉列表的值 for (int i = 0; i < strList.length; i++) { Row row = sheet.createRow(i); Cell cell = row.createCell((int) 0); cell.setCellValue(strList[i]); } workbook.setSheetHidden(workbook.getSheetIndex(name), true); XSSFDataValidationHelper dvHelper = new XSSFDataValidationHelper((XSSFSheet) workbook.getSheet(name)); dvHelper.createFormulaListConstraint(name + "!$A$1:$A$" + strList.length); return dvHelper; }
生面是xlsx的文档的添加下拉预选值,至于xls的就不展示,大概一样的道理,有问题的随时联系我。
wx:wz15713598138
qq: 2585700076