@Test
public void exportTest() throws Exception {
//导出数据
List<Map<String,Object>> list=new ArrayList<>();
//模拟动态列数据
List<Map<String,String>> userList=new ArrayList<>();
Map<String,String> userData1=new HashMap<>();
userData1.put("name","张三");
userData1.put("sex","男");
userData1.put("birthday","2019-3-15");
Map<String,String> userData2=new HashMap<>();
userData2.put("name","李四");
userData2.put("sex","女");
userData2.put("birthday","2011-4-01");
userList.add(userData1);
userList.add(userData2);
LocalDateTime nowDate=LocalDateTime.now();
DateTimeFormatter df2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String dateYMD = nowDate.format(df2);
try {
//自定义赋值行数据
Map<String, Object> listMap = new HashMap<>();
listMap.put("c1",dateYMD);
List<Map<String, Object>> c4List = new ArrayList<>();
Map<String, Object> c4Map = new HashMap<>(1);
List<Map<String, Object>> c41List = new ArrayList<>();
Map<String, Object> c41Map = new HashMap<>(1);
c41Map.put("c4-1-1",411);
List<Map<String, Object>> c42List = new ArrayList<>();
Map<String, Object> c42Map = new HashMap<>(1);
c42Map.put("c4-2-1",421);
c41List.add(c41Map);
c42List.add(c42Map);
c4Map.put("c4-1",c41List);
c4Map.put("c4-2",c42List);
c4List.add(c4Map);
listMap.put("c4",c4List);
List<Map<String, Object>> c7List = new ArrayList<>();
Map<String, Object> c7Map = new HashMap<>();
for(int orderIndex=1;orderIndex<=userList.size();orderIndex++) {
List<Map<String, Object>> c71List = new ArrayList<>();
Map<String, Object> c71Map = new HashMap<>();
c71Map.put("c7-"+orderIndex,userList.get(orderIndex-1).get("birthday"));
c71List.add(c71Map);
c7Map.put("c7-"+orderIndex,c71List);
}
c7List.add(c7Map);
listMap.put("c7",c7List);
list.add(listMap);
} catch (Exception e) {
e.printStackTrace();
}
/**
* setHeadRowsTest(userList)自定义表头
* list导出数据
*/
Workbook workbook = ExcelExportUtil.exportExcel(
new ExportParams("材料有限公司--生产报表", null),
setHeadRowsTest(userList),
list
);
File saveFile = new File("D:/");
if (!saveFile.exists()) {
saveFile.mkdirs();
}
FileOutputStream fos = new FileOutputStream("D:/testExportTitleExcel.xls");
workbook.write(fos);
fos.close();
}
/**
* 自定义表头
* @return
*/
public static List<ExcelExportEntity> setHeadRowsTest(List<Map<String,String>> userList){
List<ExcelExportEntity> entityList = new ArrayList<>();
ExcelExportEntity c1 = new ExcelExportEntity("日期", "c1");
c1.setOrderNum(1);
c1.setWidth(20);
entityList.add(c1);
ExcelExportEntity c4 = new ExcelExportEntity("公用工程消耗", "c4");
List<ExcelExportEntity> c4s = new ArrayList<>();
ExcelExportEntity c4s1 = new ExcelExportEntity("水", "c4-1");
List<ExcelExportEntity> c4s11 = new ArrayList<>();
c4s11.add(new ExcelExportEntity("(kg)", "c4-1-1"));
c4s1.setList(c4s11);
c4s.add(c4s1);
ExcelExportEntity c4s2 = new ExcelExportEntity("电", "c4-2");
List<ExcelExportEntity> c4s21 = new ArrayList<>();
c4s21.add(new ExcelExportEntity("(kW·h)", "c4-2-1"));
c4s2.setList(c4s21);
c4s.add(c4s2);
ExcelExportEntity c4s3 = new ExcelExportEntity("燃气", "c4-3");
List<ExcelExportEntity> c4s31 = new ArrayList<>();
c4s31.add(new ExcelExportEntity("(Nm³)", "c4-3-1"));
c4s3.setList(c4s31);
c4s.add(c4s3);
c4.setList(c4s);
c4.setOrderNum(5);
entityList.add(c4);
ExcelExportEntity c7 = new ExcelExportEntity("出库(包/吨)", "c7");
List<ExcelExportEntity> c7s = new ArrayList<>();
int firstNumber=1;
for(Map<String,String> userData:userList){
ExcelExportEntity clientEntity = new ExcelExportEntity("("+firstNumber+")", "c7-"+firstNumber);
List<ExcelExportEntity> clientEntityList = new ArrayList<>();
clientEntityList.add(new ExcelExportEntity(userData.get("name"), "c7-"+firstNumber));
clientEntity.setList(clientEntityList);
c7s.add(clientEntity);
firstNumber++;
}
c7.setList(c7s);
c7.setOrderNum(2);
entityList.add(c7);
return entityList;
}
<!-- easypoi版本 -->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>4.3.0</version>
</dependency>
还在想