easyexcel怎么设置表头宽度_easyexcel 自动设置列宽

这篇博客介绍了如何在EasyExcel中自定义表头宽度,通过创建`CustomCellWriteHandler`类,根据单元格内容长度动态设置列宽。在导出数据时,通过`EasyExcel.write().head().registerWriteHandler()`方法应用该处理器,确保列宽适应内容长度。
摘要由CSDN通过智能技术生成

版本

com.alibaba

easyexcel

2.1.4

导出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 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> CACHE = new HashMap<>();

@Override

protected void setColumnWidth(WriteSheetHolder writeSheetHolder, List cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {

boolean needSetWidth = isHead || !CollectionUtils.isEmpty(cellDataList);

if (needSetWidth) {

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

}

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值