手动拼装复杂表头
目录
导出excel的时候,有时候遇到要导出复杂的表头
有规律的复杂表头,可以动态拼接。没有规律的,就需要手动处理了。
先理解下表格的位置,二维的,从0开始,依次增加。
表格1:理解表格的位置
0 | 1 | 2 | 3 | 4 |
1 |
|
|
|
|
2 |
|
|
|
|
3 |
|
|
|
|
4 |
|
|
|
|
5 |
|
|
|
|
红色表示单个的位置
合并语法:
new CellRangeAddress(int firstRow, int lastRow, int firstCol, int lastCol);
为合并单元格操作方式,
四个参数分别是:起始行,结束行,起始列,结束列 。
如果是单个的,不用设置CellRangeAddress,不然会报错的
只要设置需要合并的表头!!!
表格2:
0-1 | 2-4 | |||
1 |
|
|
|
|
2 |
|
|
|
|
3 |
|
|
|
|
4 |
|
|
|
|
5 |
|
|
|
|
Range:
0-1: new CellRangeAddress(0, 0, 0, 1);
2-4: new CellRangeAddress(0, 0, 2, 4);
标题设置代码:
List<String> titleOne = Lists.newArrayList("日期","","订单总量","","");
有几列就写几个,保持列一致,占位的内容补空
range设置代码:
private List<CellRangeAddress> addNoMergeTitle() {
int firstRow = 0, lastRow = 0, firstCol = 0, lastCol = 0;
List<CellRangeAddress> cellRangeAddressList = new ArrayList<>();
cellRangeAddressList.add(new CellRangeAddress(firstRow, lastRow, firstCol, lastCol+1));
cellRangeAddressList.add(new CellRangeAddress(firstRow, lastRow, firstCol+2, lastCol+4));
return cellRangeAddressList;
}
结果:
表格2:
0-4 | ||||
1 | 1-2 | 3-4 | ||
2 |
|
|
|
|
3 |
|
|
|
|
4 |
|
|
|
|
5 |
|
|
|
|
Range:
第一行:
0-4: new CellRangeAddress(0, 0, 0, 4);
第二行:
1-2:new CellRangeAddress(1, 1, 1, 2);
3-4: new CellRangeAddress(1, 1, 3, 4);
标题设置代码:
List<String> titleOne = Lists.newArrayList("统计","","","","");
List<String> titleTwo = Lists.newArrayList("","日期","","订单总量","");
其它的内容补空
range设置代码:
private List<CellRangeAddress> addMergeTwo() {
List<CellRangeAddress> cellRangeAddressList = new ArrayList<>();
cellRangeAddressList.add(new CellRangeAddress(0, 0, 0, 4));
cellRangeAddressList.add(new CellRangeAddress(1, 1, 1, 2));
cellRangeAddressList.add(new CellRangeAddress(1, 1, 3, 4));
return cellRangeAddressList;
}
结果:
表格3:
0-4 | ||||
1-3 | 1-4 | |||
|
|
|
| |
4 |
|
|
|
|
5 |
|
|
|
|
Range:
第一行:
0-4: new CellRangeAddress(0, 0, 0, 4);
第二行:
1-4:new CellRangeAddress(1, 1, 1, 4);
第三行:
1-3: new CellRangeAddress(1, 2, 0, 0);
从上往下一行一行设置,跨行的,合并留给下一行设置,不容易搞混。
标题设置代码:
List<String> titleOne = Lists.newArrayList("统计","","","","");
List<String> titleTwo = Lists.newArrayList("日期","综合","","","");
List<String> titleThree = Lists.newArrayList("","订单总量","异常量","超时量","及时率");
这边特别注意表题的设置,要从上往下设置,先出现,先设置,比如:日期处于第二和第三行,以上一行为准
range设置代码:
private List<CellRangeAddress> addMergeThree() {
List<CellRangeAddress> cellRangeAddressList = new ArrayList<>();
cellRangeAddressList.add(new CellRangeAddress(0, 0, 0, 4));
cellRangeAddressList.add(new CellRangeAddress(1, 1, 1, 4));
cellRangeAddressList.add(new CellRangeAddress(1, 2, 0, 0));
return cellRangeAddressList;
}
结果:
表格4:
0-4 | ||||
1-3 | 1-4 | |||
1-2 | 3-4 | |||
|
|
|
| |
4 |
|
|
|
|
5 |
|
|
|
|
Range:
第一行:
0-4: new CellRangeAddress(0, 0, 0, 4);
第二行:
1-4:new CellRangeAddress(1, 1, 1, 4);
第三行:
1-2: new CellRangeAddress(2, 2, 1, 2);
3-4: new CellRangeAddress(2, 2, 3, 4);
第四行:
1-4:new CellRangeAddress(1, 3, 0, 0);
从上往下一行一行设置,跨行的,留给下一行设置,不容易搞混。
标题设置代码:
List<String> titleOne = Lists.newArrayList("统计","","","","");
List<String> titleTwo = Lists.newArrayList("日期","综合","","","");
List<String> titleThree = Lists.newArrayList("","数值","","比率","");
List<String> titleFour = Lists.newArrayList("","订单总量","异常量","及时率","成功率");
这边特别注意表题的设置,日期处于第二和第三行,以上一行为准
range设置代码:
private List<CellRangeAddress> addMergeFour() {
List<CellRangeAddress> cellRangeAddressList = new ArrayList<>();
cellRangeAddressList.add(new CellRangeAddress(0, 0, 0, 4));
cellRangeAddressList.add(new CellRangeAddress(1, 1, 1, 4));
cellRangeAddressList.add(new CellRangeAddress(2, 2, 1, 2));
cellRangeAddressList.add(new CellRangeAddress(2, 2, 3, 4));
cellRangeAddressList.add(new CellRangeAddress(1, 3, 0, 0));
return cellRangeAddressList;
}
结果:
总结:
手动拼接复杂表头的时候,设置合并CellRangeAddress,要一行一行拼接,从上而下。出现跨行的,后设置。
设置表题名的时候,一行一行设置,跨行的,先出现的,先设置(这边跟合并的设置有区别),要保持位置一致,占位的内容置空。