概述
java 动态导出excel表单 无模板本地生成
这里使用的是alibaba的公共类excelWriter,注意在pom文件中要引入easyExcel的依赖
public void exportExcel(Long signupId){
MettingSignup mettingSignup = mettingSignupService.getById(signupId);
// 文件输出位置
String outPath = "D:\"+mettingSignup.getPageName()+".xlsx";
List<Map<String, Object>> mapLists = signupUserinfoService.selectBySignupId(signupId,1,1000);
try (
FileOutputStream fileOutputStream = new FileOutputStream(outPath);
) {
// 所有行的集合
List<List<Object>> list = new ArrayList<List<Object>>();
ExcelWriter excelWriter = EasyExcelFactory.getWriter(fileOutputStream);
// 表单
Sheet sheet = new Sheet(1,0);
sheet.setSheetName("报名表单");
// 创建一个表格
Table table = new Table(1);
// 动态添加 表头 headList --> 所有表头行集合
List<List<String>> headList = new ArrayList<List<String>>();
List<Map> mapList = formTemplateService.selectFormTitleBySignupId(signupId);
// 第 n 行 的表头
for (Map map:mapList) {
List<String> headTitle = new ArrayList<String>();
headTitle.add((String) map.get("form_title"));
headList.add(headTitle);
}
table.setHead(headList);
// 第 n 行的数据
for (Map<String,Object> maps : mapLists) {
List<Object> row = new ArrayList<Object>();
for (Map map:mapList) {
row.add(maps.get(map.get("form_title")));
}
list.add(row);
}
excelWriter.write1(list,sheet,table);
// 记得 释放资源
excelWriter.finish();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
最后
以上就是甜甜高山为你收集整理的java 动态导出excel表单 无模板本地生成的全部内容,希望文章能够帮你解决java 动态导出excel表单 无模板本地生成所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复