springboot业务功能实战(十三)poi导出并以web下载方式保存excel
【摘要】
/** * 人员导出 * * @param sysSysUserVO * @throws IOException */ @PostMapping("/exportUser") @ApiOperation(value = "导出人员excel", notes = "导出人员excel")...
-
/**
-
* 人员导出
-
*
-
* @param sysSysUserVO
-
* @throws IOException
-
*/
-
@PostMapping("/exportUser")
-
@ApiOperation(value = "导出人员excel", notes = "导出人员excel")
-
public void
-
exportUser(@ApiParam(name = "人员id", value = "人员id", required = false) @RequestBody List<SysUserVO> sysSysUserVO)
-
throws IOException {
-
List<SysUserVO> middleList = new ArrayList<>();
-
// 查询用户详细信息
-
for (SysUserVO sysUserVO : sysSysUserVO) {
-
List<SysUserVO> resultListSysUser = sysUserService.querySysUserAll(sysUserVO);
-
userInfoUtil.completionInformation(resultListSysUser.get(0));
-
middleList.add(resultListSysUser.get(0));
-
}
-
// excle格式
-
String[] headers = {"用户名", "姓名", "密码", "启动状态", "岗位", "角色", "所属部门", "手机", "邮箱", "身份证号"};
-
HSSFWorkbook workbook = new HSSFWorkbook();
-
HSSFSheet sheet = workbook.createSheet();
-
// 设置列宽
-
sheet.setDefaultColumnWidth((short)18);
-
HSSFRow row = sheet.createRow(0);
-
for (short i = 0; i < headers.length; i++) {
-
// 创建单元格,每行多少数据就创建多少个单元格
-
HSSFCell cell = row.createCell(i);
-
HSSFRichTextString text = new HSSFRichTextString(headers[i]);
-
// 给单元格设置内容
-
cell.setCellValue(text);
-
}
-
for (int j = 0; j < middleList.size(); j++) {
-
SysUserVO export = middleList.get(j);
-
// 从第二行开始填充数据
-
row = sheet.createRow(j + 1);
-
List<String> datas = new ArrayList<>();
-
String userName = export.getUsername();
-
String trueName = export.getTureName();
-
String password = export.getPassword();
-
String status = String.valueOf(export.getIsEnabled());
-
String postName = export.getPostName();
-
String roleName = export.getRoleName();
-
String organName = export.getOrganizationName();
-
String phone = export.getMobile();
-
String email = export.getEmail();
-
String identityCard = export.getIdentityCard();
-
datas.add(userName);
-
datas.add(trueName);
-
datas.add(password);
-
datas.add(status);
-
datas.add(postName);
-
datas.add(roleName);
-
datas.add(organName);
-
datas.add(phone);
-
datas.add(email);
-
datas.add(identityCard);
-
for (int k = 0; k < datas.size(); k++) {
-
String string = datas.get(k);
-
HSSFCell cell = row.createCell(k);
-
HSSFRichTextString richString = new HSSFRichTextString(string);
-
HSSFFont font3 = workbook.createFont();
-
// 定义Excel数据颜色,这里设置为蓝色
-
font3.setColor(HSSFColor.BLUE.index);
-
richString.applyFont(font3);
-
cell.setCellValue(richString);
-
}
-
}
-
String fileName = "人员导出.xls";
-
// 导出
-
HttpServletResponse response =
-
((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getResponse();
-
RequestAttributes requsetAttributes = RequestContextHolder.currentRequestAttributes();
-
HttpServletRequest request = ((ServletRequestAttributes)requsetAttributes).getRequest();
-
// 获得浏览器代理信息
-
final String userAgent = request.getHeader("USER-AGENT");
-
// 判断浏览器代理并分别设置响应给浏览器的编码格式
-
if (StringUtils.contains(userAgent, "MSIE") || StringUtils.contains(userAgent, "Trident")) {
-
// IE浏览器
-
fileName = URLEncoder.encode(fileName, "UTF-8");
-
} else if (StringUtils.contains(userAgent, "Mozilla")) {
-
// google,火狐浏览器
-
fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");
-
} else {
-
// 其他浏览器
-
fileName = URLEncoder.encode(fileName, "UTF-8");// 其他浏览器
-
}
-
// 设置HTTP响应头
-
response.reset();
-
// 重置 如果不在页面上显示而是下载下来 则放开注释
-
response.setContentType("application/octet-stream");
-
response.addHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
-
OutputStream os = response.getOutputStream();
-
workbook.write(os);
-
os.close();
-
}
文章来源: baocl.blog.csdn.net,作者:小黄鸡1992,版权归原作者所有,如需转载,请联系作者。
原文链接:baocl.blog.csdn.net/article/details/115063026
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)