easypoi添加下拉预选值

原因是使用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

原文 

https://www.maiyewang.com/archives/101710

本站部分文章源于互联网,本着传播知识、有益学习和研究的目的进行的转载,为网友免费提供。如有著作权人或出版方提出异议,本站将立即删除。如果您对文章转载有任何疑问请告之我们,以便我们及时纠正。

PS:推荐一个微信公众号: askHarries 或者qq群:474807195,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

转载请注明原文出处:Harries Blog™ » easypoi添加下拉预选值

赞 (0)
分享到:更多 ()

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址