• easyexcel 自动设置列宽(转)


    来源:oschina

    链接:https://my.oschina.net/u/3574106/blog/4287753

        <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>easyexcel</artifactId>
                <version>2.1.4</version>
            </dependency>

    导出controller层代码

     @RequestMapping("/download")
        public void download(HttpServletResponse response) throws IOException {
    
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
    
            String fileName = URLEncoder.encode("测试", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            List<Dept> depts = new ArrayList<>(3);
            for (int i = 0; i < 3; i++) {
                Dept dept = new Dept();
                dept.setDname("d"+i);
                dept.setDeptno(i);
                dept.setDbDource("s"+i);
                dept.setEmpname("0000000000000000000000000000000000000000000e"+i);
                depts.add(dept);
            }
            EasyExcel.write(response.getOutputStream(), Dept.class)
                    .head(Dept.class).registerWriteHandler(new CustomCellWriteHandler())
                    .sheet("模板").doWrite(depts);
        }

    dept

    import com.alibaba.excel.annotation.ExcelProperty;
    
    public class Dept {
        @ExcelProperty(value = "部门编号")
        private Integer deptno;
        @ExcelProperty(value = "部门名称")
        private String dname;
        @ExcelProperty(value = "部门来源")
        private String dbDource;
        @ExcelProperty(value = "员工名")
        private String empname;
    // 省略get/set
    writeHandler
    
    package com.example.springbootbasic.config;
    
    import com.alibaba.excel.enums.CellDataTypeEnum;
    import com.alibaba.excel.metadata.CellData;
    import com.alibaba.excel.metadata.Head;
    import com.alibaba.excel.util.CollectionUtils;
    import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
    import com.alibaba.excel.write.style.column.AbstractColumnWidthStyleStrategy;
    import org.apache.poi.ss.usermodel.Cell;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class CustomCellWriteHandler extends AbstractColumnWidthStyleStrategy {
    
        private static final Logger LOGGER = LoggerFactory.getLogger(CustomCellWriteHandler.class);
    
        private Map<Integer, Map<Integer, Integer>> CACHE = new HashMap<>();
    
        @Override
        protected void setColumnWidth(WriteSheetHolder writeSheetHolder, List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
            boolean needSetWidth = isHead || !CollectionUtils.isEmpty(cellDataList);
            if (needSetWidth) {
                Map<Integer, Integer> maxColumnWidthMap = CACHE.get(writeSheetHolder.getSheetNo());
                if (maxColumnWidthMap == null) {
                    maxColumnWidthMap = new HashMap<>();
                    CACHE.put(writeSheetHolder.getSheetNo(), maxColumnWidthMap);
                }
    
                Integer columnWidth = this.dataLength(cellDataList, cell, isHead);
                if (columnWidth >= 0) {
                    if (columnWidth > 255) {
                        columnWidth = 255;
                    }
    
                    Integer maxColumnWidth = maxColumnWidthMap.get(cell.getColumnIndex());
                    if (maxColumnWidth == null || columnWidth > maxColumnWidth) {
                        maxColumnWidthMap.put(cell.getColumnIndex(), columnWidth);
                        writeSheetHolder.getSheet().setColumnWidth(cell.getColumnIndex(), columnWidth * 256);
                    }
    
                }
            }
        }
    
        private Integer dataLength(List<CellData> cellDataList, Cell cell, Boolean isHead) {
            if (isHead) {
                return cell.getStringCellValue().getBytes().length;
            } else {
                CellData cellData = cellDataList.get(0);
                CellDataTypeEnum type = cellData.getType();
                if (type == null) {
                    return -1;
                } else {
                    switch (type) {
                        case STRING:
                            return cellData.getStringValue().getBytes().length;
                        case BOOLEAN:
                            return cellData.getBooleanValue().toString().getBytes().length;
                        case NUMBER:
                            return cellData.getNumberValue().toString().getBytes().length;
                        default:
                            return -1;
                    }
                }
            }
        }
    }
  • 相关阅读:
    利用JQuery一步步打造无缝滚动新闻
    asp.net操作oracle存储过程的小问题
    C# Tostring() 格式大全 [转]
    Oracle存储过程总结【转】
    JQuery中text()、html()和val()的区别
    JQuery的JSON数据格式
    HDU 1065 I Think I Need a Houseboat
    POJ1251 Jungle Roads
    HDU3177 Crixalis's Equipment
    HDU1042 N!
  • 原文地址:https://www.cnblogs.com/lshan/p/15075081.html
Copyright © 2020-2023  润新知