版本

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;

}

}

}

}

}

GitHub 加速计划 / ea / easyexcel
26
5
下载
快速、简洁、解决大文件内存溢出的java处理Excel工具
最近提交(Master分支:4 个月前 )
c42183df Bugfix 1 年前
efa7dff6 * 重新加回 `commons-io` 1 年前
Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐