上次说了EasyExcel如何实现excel台帐导出,这次说一下另一个工具ExcelWriter,直接看代码吧
@Override
public void exportPersonExcel(HttpServletResponse response) throws IOException {
//获取联系人列表
List<Person> contacts = personMapper.listPersons();
//new 一个write对象
ExcelWriter writer = new ExcelWriter(true);
//合并首行,起个标题(根据需要)
writer.merge(13, "人员管理清单");
//设置标题栏
writer.addHeaderAlias("dep", "单位");
writer.addHeaderAlias("duty", "职务");
writer.addHeaderAlias("name", "姓名");
writer.addHeaderAlias("mobile", "手机号码");
//只导出设置别名的字段 写的位置好像影响效果 奇葩
writer.setOnlyAlias(true);
//设置样式,宽度
writer.setColumnWidth(0, 20);
writer.setColumnWidth(1, 8);
writer.setColumnWidth(2, 10);
writer.setColumnWidth(3, 13);
//放入数据
writer.write(contacts, true);
//导出过程
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
ServletOutputStream out = null;
try {
String fileName = URLEncoder.encode("人员管理清单", "UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
out = response.getOutputStream();
writer.flush(out, true);
} catch (IOException e) {
e.printStackTrace();
} finally {
//一定记得关闭 养成好习惯
writer.close();
IoUtil.close(out);
}
}