/**
* @author Wilson
* @date 2019/8/26
**/
public class DynamicPoiTest {
public static void main(String[] args) throws IOException {
List<ExcelExportEntity> entity = new ArrayList<>();
entity.add(buildExcelEntity("序号", "order", 0));
entity.add(buildExcelEntity("性别", "sex", 1));
// 设置了group的若不设置排序默认会派到最后
entity.add(buildExcelEntityByGroup("姓名", "students.name", "学生", 2));
entity.add(buildExcelEntityByGroup("性别", "students.sex", "学生", 2));
entity.add(buildExcelEntity("班级", "class", 3));
List<Map<String, Object>> list = new ArrayList<>();
IntStream.range(0, 10).forEach(i -> {
Map<String, Object> params = new HashMap<>();
params.put("order", "" + i);
params.put("sex", "sex" + i);
params.put("class", "class-" + i);
params.put("students.sex", "man-" + i);
params.put("students.name", "name-" + i);
list.add(params);
});
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("测试", "测试"), entity, list);
try (FileOutputStream fos = new FileOutputStream("E:/IntelliJ/workspace/netty-demo/ExcelExportForMap.xls")) {
workbook.write(fos);
}
}
private static ExcelExportEntity buildExcelEntity(String name, Object key, int orderNo) {
ExcelExportEntity excelEntity = new ExcelExportEntity(name, key);
excelEntity.setOrderNum(orderNo);
return excelEntity;
}
/**
* 在group列下添加子列
*
* @param name
* @param key
* @param group
* @return
*/
private static ExcelExportEntity buildExcelEntityByGroup(String name, Object key, String group, int orderNo) {
ExcelExportEntity excelEntity = new ExcelExportEntity(name, key);
excelEntity.setGroupName(group);
excelEntity.setOrderNum(orderNo);
return excelEntity;
}
}