动态表头导出excel
红框固定,绿框动态
引入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.1.1</version>
</dependency>
工具类
import com.alibaba.excel.util.ListUtils;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.mcsgis.saas.common.data.DynamicExcel;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import java.util.List;
/**
* @author: xianyu
* @createDate: 2024/6/6
* @description: 动态表头excel导出
*/
public class DynamicExcelUtil {
/**
* 设置表头
*
* @param dynamicExcel
* @return
*/
public static List<List<String>> head(DynamicExcel dynamicExcel) {
List<List<String>> headTitles = ListUtils.newArrayList();
String empty = " ";
//表头可以根据实际情况进行修改
List<String> fixedColumn = dynamicExcel.getFixedColumn();
for (String s : fixedColumn) {
headTitles.add(ListUtils.newArrayList(s));
}
//一级表头
List<String> mealList = dynamicExcel.getMealList();
//二级表头
List<String> foodList = dynamicExcel.getFoodList();
// 根据实际需要,决定要渲染多少列
mealList.forEach(meal -> {
foodList.forEach(food -> {
headTitles.add(ListUtils.newArrayList(meal, food));
});
});
return headTitles;
}
/**
* 配置字体,表头背景等
*
* @return
*/
public static HorizontalCellStyleStrategy setConfigure() {
// 头的策略
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
// 背景色
headWriteCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
WriteFont headWriteFont = new WriteFont();
// 加粗
headWriteFont.setBold(true);
headWriteCellStyle.setWriteFont(headWriteFont);
// 内容的策略
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
// 字体策略
WriteFont contentWriteFont = new WriteFont();
// 字体大小
//contentWriteFont.setFontHeightInPoints((short) 14);
contentWriteCellStyle.setWriteFont(contentWriteFont);
//边框
//导出数据垂直居中
contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
//导出数据水平居中
contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
//设置 自动换行
contentWriteCellStyle.setWrapped(true);
//设置
// 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
return new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
}
}
调用
@GetMapping("load")
public void downLoad(HttpServletResponse response) throws IOException {
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
String fileName = URLEncoder.encode("按收费渠道", "UTF-8").replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
ExcelWriter writer = EasyExcelFactory.write(response.getOutputStream())
//列宽
.registerWriteHandler(new SimpleColumnWidthStyleStrategy(16))
.registerWriteHandler(new SimpleRowHeightStyleStrategy((short) 40, (short) 30))
// 核心代码:表头和正文的样式在此
.registerWriteHandler(DynamicExcelUtil.setConfigure())
.build();
// 动态添加表头,适用一些表头动态变化的场景
WriteSheet sheet1 = new WriteSheet();
sheet1.setSheetName("sheet001");
sheet1.setSheetNo(0);
DynamicExcel dynamicExcel = new DynamicExcel();
//固定列
List<String> fixedColumn = new ArrayList<>();
fixedColumn.add("项目名称");
fixedColumn.add("收费日期");
//一级表头
List<String> mealList = new ArrayList<>();
mealList.add("线下-线下支付");
mealList.add("线下微信扫码");
mealList.add("微信小程序");
mealList.add("PC微信");
mealList.add("合计");
//二级表头
List<String> foodList = new ArrayList<>();
foodList.add("实收金额");
foodList.add("手续费金额");
foodList.add("分账金额");
foodList.add("实际到账金额");
//内容
List<List<Object>> contentList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
List<Object> content = new ArrayList<>();
content.add(1);
content.add(2);
content.add(3);
content.add(4);
content.add(5);
content.add(6);
content.add(7);
content.add(8);
content.add(9);
content.add(10);
content.add(11);
content.add(12);
content.add(13);
content.add(14);
content.add(15);
content.add(16);
content.add(17);
content.add(18);
content.add(19);
content.add(20);
content.add(21);
content.add(22);
contentList.add(content);
}
dynamicExcel.setFixedColumn(fixedColumn);
dynamicExcel.setMealList(mealList);
dynamicExcel.setFoodList(foodList);
dynamicExcel.setContentList(contentList);
// 创建一个表格,用于 Sheet 中使用
WriteTable table = new WriteTable();
table.setTableNo(1);
// 核心代码:设置表头
table.setHead(DynamicExcelUtil.head(dynamicExcel));
// 写数据
writer.write(dynamicExcel.getContentList(), sheet1, table);
writer.finish();
}
结果