表头可以根据业务进行设置,然后后续赋值对应的值。
@Test
public void easyExcelTest() {
List<List<String>> heads = Lists.newArrayList();
heads.add(Lists.newArrayList("表头1"));
heads.add(Lists.newArrayList("表头2"));
heads.add(Lists.newArrayList("表头3"));
heads.add(Lists.newArrayList("表头4"));
heads.add(Lists.newArrayList("表头5"));
List<List<String>> contents = Lists.newArrayList();
for (int i = 0; i <= 10; i++) {
List<String> content = Lists.newArrayList();
for (int j = 0; j < 5; j++) {
content.add("第" + i + "行第" + j + "例内容");
}
contents.add(content);
}
// 表头样式策略
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
// 设置数据格式
headWriteCellStyle.setDataFormat((short) BuiltinFormats.getBuiltinFormat("m/d/yy h:mm"));
// 是否换行
headWriteCellStyle.setWrapped(false);
// 水平对齐方式
headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);
// 垂直对齐方式
headWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
// 前景色
headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
// 背景色
headWriteCellStyle.setFillBackgroundColor(IndexedColors.WHITE.getIndex());
// 设置为1时,单元格将被前景色填充
headWriteCellStyle.setFillPatternType(FillPatternType.NO_FILL);
// 控制单元格是否应自动调整大小以适应文本过长时的大小
headWriteCellStyle.setShrinkToFit(false);
// 单元格边框类型
headWriteCellStyle.setBorderBottom(BorderStyle.NONE);
headWriteCellStyle.setBorderLeft(BorderStyle.NONE);
headWriteCellStyle.setBorderRight(BorderStyle.NONE);
headWriteCellStyle.setBorderTop(BorderStyle.NONE);
// 单元格边框颜色
headWriteCellStyle.setLeftBorderColor(IndexedColors.BLACK.index);
headWriteCellStyle.setRightBorderColor(IndexedColors.BLACK.index);
headWriteCellStyle.setTopBorderColor(IndexedColors.BLACK.index);
headWriteCellStyle.setBottomBorderColor(IndexedColors.BLACK.index);
// 字体策略
WriteFont writeFont = new WriteFont();
// 是否加粗/黑体
writeFont.setBold(false);
// 字体颜色
writeFont.setColor(Font.COLOR_NORMAL);
// 字体名称
writeFont.setFontName("宋体");
// 字体大小
writeFont.setFontHeightInPoints((short) 11);
// 是否使用斜体
writeFont.setItalic(false);
// 是否在文本中使用横线删除
writeFont.setStrikeout(false);
// 设置要使用的文本下划线的类型
writeFont.setUnderline(Font.U_NONE);
// 设置要使用的字符集
writeFont.setCharset(FontCharset.DEFAULT.getNativeId());
headWriteCellStyle.setWriteFont(writeFont);
// 内容样式策略策略
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
contentWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.GENERAL);
contentWriteCellStyle.setBorderBottom(BorderStyle.NONE);
contentWriteCellStyle.setBorderLeft(BorderStyle.NONE);
contentWriteCellStyle.setBorderRight(BorderStyle.NONE);
contentWriteCellStyle.setBorderTop(BorderStyle.NONE);
contentWriteCellStyle.setFillPatternType(FillPatternType.NO_FILL);
contentWriteCellStyle.setWrapped(false);
EasyExcel.write("D:\\test.xlsx").head(heads)
.registerWriteHandler(new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle))
.registerWriteHandler(new SimpleColumnWidthStyleStrategy(16)) // 列宽
.registerConverter(new LocalDateTimeConverter())
.sheet("销售订单").doWrite(contents);
// 通过接口调用,使用流生成文件
/*String fileName = "导出测试";
response.setCharacterEncoding("utf-8");
fileName = URLEncoder.encode(fileName, "UTF-8") + ".xlsx";
response.setHeader("Content-disposition", "attachment;filename=" + fileName);
response.setHeader("content-Type", "application/vnd.ms-excel");
response.setHeader("filename", fileName);
EasyExcel.write(response.getOutputStream()).head(heads)
.registerWriteHandler(new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle))
.registerWriteHandler(new SimpleColumnWidthStyleStrategy(16)) // 列宽
.registerConverter(new LocalDateTimeConverter())
.sheet("销售订单").doWrite(contents);*/
}