public static void main(String[] args) {
String filePath = "C:\\Users\\Mac\\Desktop\\314.xlsx";
List<List<String>> dataset = new ArrayList<List<String>>();
for (int i = 0; i < 50; i++) {
for (int z = 0; z < 10000; z++) {
List<String> row = new ArrayList<String>();
for (int j = 0; j < 10; j++) {
row.add("你好:" + i + "-" + j);
}
dataset.add(row);
}
}
/* 自定义样式 */
// 头的策略
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
// 背景设置为红色
WriteFont headWriteFont = new WriteFont();
headWriteFont.setFontName("Courier New");
headWriteFont.setFontHeightInPoints((short) 11);
headWriteCellStyle.setWriteFont(headWriteFont);
headWriteCellStyle.setBorderBottom(BorderStyle.THIN);
headWriteCellStyle.setBorderLeft(BorderStyle.THIN);
headWriteCellStyle.setBorderRight(BorderStyle.THIN);
headWriteCellStyle.setBorderTop(BorderStyle.THIN);
headWriteCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.index);
// 设置水平对齐的样式为居中对齐;
headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
// 设置垂直对齐的样式为居中对齐;
headWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
// 内容的策略
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
// 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND 不然无法显示背景颜色.头默认了 FillPatternType所以可以不指定
contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
// 设置垂直对齐的样式为居中对齐;
contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
WriteFont contentWriteFont = new WriteFont();
contentWriteFont.setFontName("Courier New");
contentWriteFont.setFontHeightInPoints((short) 10);
contentWriteCellStyle.setWriteFont(contentWriteFont);
contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
// 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
// 头
List<List<String>> head = new ArrayList<>();
for (int i = 0; i < dataset.get(0).size(); i++) {
List<String> title = new ArrayList<>();
title.add("标题");
title.add("测试" + i);
head.add(title);
}
**// 加样式导出=内存占用大
EasyExcel.write(filePath).registerWriteHandler(horizontalCellStyleStrategy).head(head).sheet("模板").doWrite(dataset);
// 不加样式导出=内存占用小
//EasyExcel.write(filePath).head(head).sheet("模板").doWrite(dataset);**
}