zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Tensorflowlite移植ARM平台iMX6
    人生信条集
    浅谈聚类
    常用距离度量方法大全
    sklearn学习小结
    SpringBoot 2.x版本+MultipartFile设置指定文件上传大小
    SpringBoot无法访问webapp目录下的文件
    idea搜索不到任何插件
    Caused by: org.springframework.data.mapping.PropertyReferenceException: No property id found for type Users!
    Annotation-specified bean name 'userDaoImpl' for bean class [***] conflicts with existing, non-compatible bean definition of same name and class [***]
  • 原文地址:https://www.cnblogs.com/wadmwz/p/13291711.html
Copyright © 2011-2022 走看看