带下拉列表导出(easypoi)

依赖
<dependency>
   <groupId>cn.hutool</groupId>
   <artifactId>hutool-all</artifactId>
   <version>5.4.0</version>
</dependency>
<!-- easypoi -->
<dependency>
       <groupId>cn.afterturn</groupId>
       <artifactId>easypoi-base</artifactId>
       <version>3.2.0</version>
   <exclusions>
      <exclusion>
         <artifactId>guava</artifactId>
         <groupId>com.google.guava</groupId>
      </exclusion>
   </exclusions>
</dependency>
<dependency>
       <groupId>cn.afterturn</groupId>
       <artifactId>easypoi-web</artifactId>
       <version>3.2.0</version>
</dependency>
<dependency>
       <groupId>cn.afterturn</groupId>
       <artifactId>easypoi-annotation</artifactId>
       <version>3.2.0</version>
</dependency>
工具
/**
 * excel 导出-带下拉类表
 *
 * @param response
 * @param fileName    文件名
 * @param projects    对象集合
 * @param columnNames 导出的excel中的列名
 * @param keys        对应的是对象中的字段名字
 * @param dvArray        下拉类表信息key--> 列;value--> 下拉数据
 * @throws IOException
 */
public static void exportExcelDv(HttpServletResponse response, String fileName, List<?> projects, Object[] keys, Object[] columnNames, Map<Integer,String[]> dvArray) throws IOException {
    ExcelWriter bigWriter = ExcelUtil.getBigWriter();
    Sheet sheet = bigWriter.getSheet();
    for (int i = 0; i < keys.length; i++) {
        bigWriter.addHeaderAlias(keys[i]+"",columnNames[i]+"");
        bigWriter.setColumnWidth(i, 20);
        String[] arr = dvArray.get(i);
        if(null != arr && arr.length > 0){
            DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper();
            DataValidationConstraint constraint = dataValidationHelper.createExplicitListConstraint(arr);
            DataValidation validation = dataValidationHelper.createValidation(constraint, new CellRangeAddressList(1, 65536,i , i));
            validation.createErrorBox("提示","输入有误,请选择");
            if (validation instanceof XSSFDataValidation) {
                validation.setShowErrorBox(true);
                validation.setSuppressDropDownArrow(true);
            } else {
                validation.setSuppressDropDownArrow(false);
            }
            bigWriter.addValidationData(validation);
        }
    }
    // 一次性写出内容,使用默认样式,强制输出标题
    bigWriter.write(projects, true);
    //response为HttpServletResponse对象
    response.setContentType("application/vnd.ms-excel;charset=utf-8");
    //test.xls是弹出下载对话框的文件名,不能为中文,中文请自行编码
    response.setHeader("Content-Disposition", "attachment;filename=" + new String((fileName + ".xlsx").getBytes(), "iso-8859-1"));
    ServletOutputStream out = response.getOutputStream();
    bigWriter.flush(out, true);
    // 关闭writer,释放内存
    bigWriter.close();
    //此处记得关闭输出Servlet流
    IoUtil.close(out);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值