直击要害
重点语法
//合并 * 指定 4 个参数,起始行,结束行,起始列,结束列。然后这个区域将被合并。
CellRangeAddress region02 = new CellRangeAddress(hebingkaishi, hebingjieshu, 0, 0);
s.addMergedRegion(region02);
// 关于合并的代码我封装成了方法这个方法只对此项目有用(可供参考*)
public void hebing(Workbook workbook, Map<String, Object> z, Sheet s) {
int hebingkaishi = 2;//合并从第三行开始
int hebingjieshu = 0;//合并结束
Map<String, Object> map = new HashMap<>();//存的是每个集合对应的数据条数;
Set ss = z.keySet();
Iterator iterator = ss.iterator();
while (iterator.hasNext()) {
String key = (String) iterator.next();
if ("t".equals(key)) {
continue;
}
List sss = (List) z.get(key);
int size = sss.size();
if ("hlist".equals(key)) {
hebingjieshu += hebingkaishi + size - 1;
CellRangeAddress region01 = new CellRangeAddress(hebingkaishi, hebingjieshu, 0, 0);
hebingkaishi = hebingjieshu + 1;
s.addMergedRegion(region01);
break;
}
}
Iterator iterator1 = ss.iterator();
while (iterator1.hasNext()) {
String key = (String) iterator1.next();
if ("t".equals(key)) {
continue;
}
List sss = (List) z.get(key);
int size = sss.size();
if ("tlist".equals(key)) {
hebingjieshu = hebingkaishi + size;
CellRangeAddress region02 = new CellRangeAddress(hebingkaishi, hebingjieshu, 0, 0);
hebingkaishi =hebingjieshu + 1;
s.addMergedRegion(region02);
break;
}
}
Iterator iterator2 = ss.iterator();
while (iterator2.hasNext()) {
String key = (String) iterator2.next();
if ("t".equals(key)) {
continue;
}
List sss = (List) z.get(key);
int size = sss.size();
if ("jlist".equals(key)) {
hebingjieshu = hebingkaishi + size;
CellRangeAddress region03 = new CellRangeAddress(hebingkaishi, hebingjieshu, 0, 0);
s.addMergedRegion(region03);
break;
}
}
}
// 测试类
public class Ceshi2 {
@Test
public void Export() throws Exception {
TemplateExportParams params = new TemplateExportParams("static/diyi.xls");
Map<String, Object> zong = new HashMap<String, Object>();
List<Huashi> hlist = new ArrayList<>();
List<Tuoliu> tlist = new ArrayList<>();
List<Jinggou> jlist = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Huashi h = new Huashi();
h.setName("化石燃料" + i);
h.setRezhi(i + 0.5);
h.setTanyang(i + 1.5);
hlist.add(h);
Jinggou j = new Jinggou();
j.setName("净购" + i);
j.setDanwei("平方");
j.setShuju(i + 2.5);
jlist.add(j);
Tuoliu t = new Tuoliu();
t.setDanwei("平方");
t.setName("脱硫" + i);
t.setShuju(i + 6.3);
tlist.add(t);
}
zong.put("jlist", jlist);
zong.put("hlist", hlist);
zong.put("tlist", tlist);
FileOutputStream fos;
Workbook workbook = ExcelExportUtil.exportExcel(params, zong);
Sheet s = workbook.getSheetAt(0);
hebing(workbook, zong, s);
File savefile = new File("D:/home/excel/");
if (!savefile.exists()) {
savefile.mkdirs();
}
fos = new FileOutputStream("D:/home/excel/diyi.xls");
workbook.write(fos);
fos.close();
}
}
效果图
模板
在最初我循环数据时全都用的是{{KaTeX parse error: Expected 'EOF', got '}' at position 16: fe: list t.name}̲}但是会出现报空指针错误这是因…fe: list t.name}}的意思是将下一行内容下一之后创建一行,由于是最后一行所以就错了,{{fe:list t.name}}是说只添加内容不创建行会覆盖调下一行的东西所以fe比较适用于最后一行循环,以上是我的认知确切的自己去官方看。