在 easyexcel 2.0.5 下自定义实现下拉框

在 easyexcel  2.0.5 下实现下拉框,由于easyexcel 并没有提供下拉框的api 只能自己去实现了,阿里提供一个接口WriteHandler 该接口就是用来自己自定义的功能了 
 


第一步 模型 

public class ProductExcelModel {

    @ExcelIgnore
    private String id;

    @ExcelProperty(value = "*商品名称",index = 0)
    private String goodsName;

    @ExcelProperty(value= "商品品牌",index = 1)
    private String brand;

    @ExcelProperty(value="*货号",index = 2)
    private String productCode;

    @ExcelProperty(value="*是否管制品",index = 3)
    private String dangerousMark;

    @ExcelProperty(value = "*商品类型(只允许填写:1、普通商品;2、易制爆;3、易制毒;4、剧毒品,5、精神麻醉品,6、医用毒性品、7、民用爆炸品;)",index = 4)
    private String goodType;

    @ExcelProperty(value = "*规格",index = 5)
    private String specification;

    @ExcelProperty(value = "*单位(个、盒、箱 等)",index = 6)
    private String unit;

    @ExcelProperty(value = "*发布数量",index = 7)
    private String quantity;

    @ExcelProperty(value = "CAS(危化品必填)",index = 8)
    private String casNo;

    @ExcelProperty(value="*存放校区(只允许填写:1、南校园;2、北校园;3、东校园;4、珠海区)",index = 9)
    private String campus;

    @ExcelProperty(value="商品描述(非必填项,填写商品的详细信息,包含但不限于生产厂家,开封情况,目前存放具体点。200字以内)",index = 10)
    private String remark;

}

第二步 自己定义一个Handler, 下拉框的 SpinnerWriteHandler

public class SpinnerWriteHandler implements SheetWriteHandler {

    @Override
    public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {

    }

    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
        String [] yesOrNo = new String[]{"是","否"};
        String [] productTypes = new String[]{"普通商品","易制爆","易制毒","剧毒品","精神麻醉品","医用毒性品","民用爆炸品"};
        String [] campusTypes = new String[]{"南校园","北校园","东校园","珠海区"};
        Map mapDropDown = new HashMap<>();
        mapDropDown.put(3,yesOrNo);
        mapDropDown.put(4,productTypes);
        mapDropDown.put(9,campusTypes);
        Sheet sheet = writeSheetHolder.getSheet();
        ///开始设置下拉框
        DataValidationHelper helper = sheet.getDataValidationHelper();//设置下拉框
        for (Map.Entry entry : mapDropDown.entrySet()) {
            /***起始行、终止行、起始列、终止列**/
            CellRangeAddressList addressList = new CellRangeAddressList(1, 1000, entry.getKey(), entry.getKey());
            /***设置下拉框数据**/
            DataValidationConstraint constraint = helper.createExplicitListConstraint(entry.getValue());
            DataValidation dataValidation = helper.createValidation(constraint, addressList);
            /***处理Excel兼容性问题**/
            if (dataValidation instanceof XSSFDataValidation) {
                dataValidation.setSuppressDropDownArrow(true);
                dataValidation.setShowErrorBox(true);
            } else {
                dataValidation.setSuppressDropDownArrow(false);
            }
            sheet.addValidationData(dataValidation);
        }

        //下面定时样式的 
        Row row = sheet.getRow(0);
        Workbook workbook = writeWorkbookHolder.getWorkbook();
        row.setHeight((short) 500);
        for (int i = 0; i < row.getLastCellNum(); i++) {
            sheet.setColumnWidth(i, 5000);
            Cell cell = row.getCell(i);
            cell.setCellStyle(setStyle(workbook));
        }
        row.setHeight((short) (205*7));
    }

    /***
     * 设置红色字体样式
     * @param wb
     * @return
     */
    public static CellStyle setStyle( Workbook wb){
        Font dataFont = wb.createFont();
        dataFont.setColor(HSSFColor.RED.index);
        dataFont.setFontName("宋体");
        dataFont.setFontHeight((short) 240);
        dataFont.setBold(true);
        dataFont.setFontHeightInPoints((short) 10);
        CellStyle dataStyle = wb.createCellStyle();
        dataStyle.setFont(dataFont);
        dataStyle.setWrapText(true);
        dataStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        dataStyle.setAlignment(HorizontalAlignment.CENTER);
        return dataStyle;
    }

第三步 输出文件流 
  // 这句话 就可以实现文件流的输入 
  ByteArrayOutputStream bas = new ByteArrayOutputStream();
        EasyExcelFactory.write(bas, ProductExcelModel.class)
                .registerWriteHandler(new SpinnerWriteHandler()).sheet().doWrite(new ArrayList());

这样就可以显示自己的下拉框了 如图

 

在 easyexcel 2.0.5 下自定义实现下拉框_第1张图片在 easyexcel 2.0.5 下自定义实现下拉框_第2张图片

你可能感兴趣的:(java)