⽤easypoi导出excel要合并列的⽅法
@ResponseBody
@RequestMapping(value = "excelOut")
public String excelOut(HttpServletRequest request, HttpServletResponse response) {
//建⽴⼀个模板所在⽂件夹及名称template/test.xlsx
TemplateExportParams params = new TemplateExportParams("template/test.xlsx", 0, 1);
Map<String, Object> map = new HashMap<>();
List<Map<String, String>> headList = new ArrayList<>();
//这是活动表头,如果⽤户要设置的列数是不固定的,则在这⾥设定,如取⼀个list中对象作为多个列
for (int i = 0; i < 3; i++) {
Map<String, String> m = new HashMap<>();
m.put("name", "⾃定义表头" + i);
m.put("code", "t.code" + i);
headList.add(m);
}
map.put("head", headList);
//组装数据
List<Map<String, Object>> list = new ArrayList<>();
for (int i = 0; i < 8; i++) {
nextint()方法int s = new Random().nextInt(5) + 2;
for (int k = 0; k < s; k++) {
Map<String, Object> m = new HashMap<>();
m.put("g1", "id" + i);
m.put("g2", "2固定值" + i+(i % 2 == 0 ? "" : "\t"));
m.put("g3", "3固定值" + i);
for (int j = 0; j < 3; j++) {
m.put("code" + j, i + "code" + j);
}
m.put("list_sex", i + "sex" + k);
list.add(m);
}
}
map.put("rows", list);
params.setColForEach(true);
Workbook workbook = portExcel(params, map);
//需要合并的列,相当的数据就会合并,不合并则加(i % 2 == 0 ? "" : "\t")
// FileOutputStream fos = new FileOutputStream("D:/test"+System.currentTimeMillis()+".xlsx");//存在本地磁盘
// workbook.write(fos);
// fos.close();
//设置保存的⽂件名
String fileName = Year() + "年" + Month() + "⽉" + Day() + "⽇数据表_" + System.currentTimeMillis() + ".xlsx"; //在浏览器中保存导出
response.setContentType("application/octet-stream");
try {
response.addHeader("Content-Disposition", "attachment;filename=" + de(fileName, "UTF-8"));
ServletOutputStream outputStream = OutputStream();
workbook.write(outputStream);
outputStream.flush();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
//模板图⽚,可根据⾃⼰需要更改
//建⽴⼀个模板所在⽂件夹及名称template/test.xlsx
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论