All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.xinjump.easyexcel.write.style.CustomColumnWidthStyleHandler Maven / Gradle / Ivy

The newest version!
package com.xinjump.easyexcel.write.style;

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.style.column.AbstractColumnWidthStyleStrategy;
import com.xinjump.base.util.collect.CollectionUtils;
import org.apache.poi.ss.usermodel.Cell;

import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author xinjump
 * @version 1.1
 * @date 2020/12/18 15:23
 * @see com.xinjump.easyexcel.write.style
 */
@SuppressWarnings({"rawtypes"})
public class CustomColumnWidthStyleHandler extends AbstractColumnWidthStyleStrategy {

    private final Map> CACHE = new HashMap<>();

    private static final int MAX_COLUMN_WIDTH = 255;

    @Override
    protected void setColumnWidth(WriteSheetHolder writeSheetHolder, List cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
        boolean needSetWidth = isHead || CollectionUtils.isNotEmpty(cellDataList);
        if (needSetWidth) {
            Map maxColumnWidthMap = CACHE.computeIfAbsent(writeSheetHolder.getSheetNo(), k -> new HashMap<>());

            Integer columnWidth = this.dataLength(cellDataList, cell, isHead);
            if (columnWidth >= 0) {
                if (columnWidth > MAX_COLUMN_WIDTH) {
                    columnWidth = MAX_COLUMN_WIDTH;
                } else {
                    columnWidth = columnWidth + 5;
                }

                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 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;
                }
            }
        }
    }

    /**
     * 编程方式自动表格宽度
     *
     * @param headList 表头宽度集合
     * @return
     */
    public static Map getLongestColumnWidthMap(List> headList) {
        Map columnWidth = new HashMap<>();
        for (int i = 0; i < headList.size(); i++) {
            List list = headList.get(i);
            //取最后一行表头
            String title = list.get(list.size() - 1);
            //参见 com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy.setColumnWidth
            columnWidth.put(i, title.getBytes().length * 250);
        }
        return columnWidth;
    }

    /**
     * 编程方式自动表格宽度
     *
     * @param clazz 导出实体类
     * @return
     */
    public static Map getLongestColumnWidthMap(Class clazz) {
        Field[] declaredFields = clazz.getDeclaredFields();
        List propertyList = new ArrayList<>();
        for (Field declaredField : declaredFields) {
            ExcelProperty excelProperty = declaredField.getAnnotation(ExcelProperty.class);
            if (excelProperty != null) {
                propertyList.add(excelProperty);
            }
        }
        List headList = propertyList.stream()
                .sorted(Comparator.comparing(ExcelProperty::order))
                .collect(Collectors.toList());
        Map columnWidth = new HashMap<>();
        for (int i = 0; i < headList.size(); i++) {
            ExcelProperty excelProperty = headList.get(i);
            String[] value = excelProperty.value();
            //取最后一行表头
            String title = value[value.length - 1];
            //参见 com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy.setColumnWidth
            columnWidth.put(i, title.getBytes().length * 250);
        }
        return columnWidth;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy