使用easyexcel写斜线表头
时间: 2023-10-26 15:49:13 浏览: 168
Excel斜线表头我之制作
使用EasyExcel可以很方便地实现斜线表头的效果,具体步骤如下:
1. 定义表头样式
```
CellStyle headStyle = new CellStyle();
headStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
headStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
headStyle.setAlignment(HorizontalAlignment.CENTER);
headStyle.setVerticalAlignment(VerticalAlignment.CENTER);
headStyle.setWrapText(true);
headStyle.setFont(new Font("Calibri", 12, true));
headStyle.setBorderTop(BorderStyle.THIN);
headStyle.setBorderBottom(BorderStyle.THIN);
headStyle.setBorderLeft(BorderStyle.THIN);
headStyle.setBorderRight(BorderStyle.THIN);
```
2. 定义表头内容
```
List<List<String>> head = new ArrayList<>();
List<String> head1 = new ArrayList<>();
List<String> head2 = new ArrayList<>();
List<String> head3 = new ArrayList<>();
head1.add("日期");
head2.add("2021-08-01");
head3.add("2021-08-02");
head.add(head1);
head.add(head2);
head.add(head3);
```
3. 写入Excel
```
ExcelWriter writer = EasyExcel.write("斜线表头.xlsx").build();
Sheet sheet = new Sheet(1, 0);
sheet.setHead(head);
sheet.setAutoWidth(Boolean.TRUE);
writer.write1(null, sheet);
writer.finish();
```
效果如下图所示:
![斜线表头效果图](https://cdn.luogu.com.cn/upload/image_hosting/egf1r1j4.png)
阅读全文