使用hutool工具进行导入导出excel表格2
username password nickname email
张三 23 88.32 TRUE
李四 33 59.50 FALSE
自定义内容
第五行第二列
名字 张三
密码 23
昵称 88.32
电子邮件 true
效果图:
![image](https://img2022.cnblogs.com/blog/2390012/202209/2390012-20220927153214108-978798094.png)
@GetMapping("/exportExample")
@Inner(false)
public void exportExample(CppccProposal cppccProposal, HttpServletResponse response) throws IOException {
//查询所有用户
Map<String, Object> row1 = new LinkedHashMap<>();
row1.put("username", "张三");
row1.put("password", 23);
row1.put("nickname", 88.32);
row1.put("email", true);
Map<String, Object> row2 = new LinkedHashMap<>();
row2.put("username", "李四");
row2.put("password", 33);
row2.put("nickname", 59.50);
row2.put("email", false);
ArrayList<Map<String, Object>> rows = CollUtil.newArrayList(row1, row2);
ExcelWriter writer= ExcelUtil.getWriter(true);
//自定义标题别名
// writer.addHeaderAlias("username","姓名");
// writer.addHeaderAlias("password","密码");
// writer.addHeaderAlias("nickname","昵称");
// writer.addHeaderAlias("email","对错");
//默认配置
writer.write(rows,true);
List<Object> row = ListUtil.of((CellSetter) cell -> cell.setCellValue("自定义内容"));
writer.writeRow(row);
writer.writeCellValue(1,4,"第五行第二列");
writer.writeCellValue(0,5,"名字");
writer.writeCellValue(1,5,row1.get("username").toString());
writer.writeCellValue(0,6,"密码");
writer.writeCellValue(1,6,row1.get("password").toString());
writer.writeCellValue(0,7,"昵称");
writer.writeCellValue(1,7,row1.get("nickname").toString());
writer.writeCellValue(0,8,"电子邮件");
writer.writeCellValue(1,8,row1.get("email").toString());
// writer.clearHeaderAlias();
//设置content—type
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset:utf-8");
//设置标题
String fileName= URLEncoder.encode("用户信息","UTF-8");
//Content-disposition是MIME协议的扩展,MIME协议指示MIME用户代理如何显示附加的文件。
response.setHeader("Content-Disposition","attachment;filename="+fileName+".xlsx");
ServletOutputStream outputStream= response.getOutputStream();
//将Writer刷新到OutPut
writer.flush(outputStream,true);
outputStream.close();
}
择善人而交,择善书而读,择善言而听,择善行而从。