springBoot中使用easypoi(非注解式)导出excel记录

2022-07-30,,,,

1、在实现导出excel的过程中,使用easypoi时发现网上大多都是实体类的属性上加注解的方式去导出(简单是简单,我之前的博客也有介绍这种实现),但是问题来了,如果没有实体对应的数据库数据该如何导出呢?
2、首先导入easypoi的jar包
 <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-base</artifactId> <version>3.2.0</version> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-web</artifactId> <version>3.2.0</version> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-annotation</artifactId> <version>3.2.0</version> </dependency> 
3、核心代码如下:
 // 第一个对象,新建一个工作簿 HSSFWorkbook workbook = new HSSFWorkbook(); // 设置第一个sheet的名称 HSSFSheet sheet = workbook.createSheet(widget.getEchartsTitle()); // 设置文件名以及后缀 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); String fileName = "你要设置的文件名 + "_" + sdf.format(new Date()) + ".xls"; // 开始添加excel第一行表头(excel中下标是0) HSSFRow row = sheet.createRow(0); // 设置excel宽度和高度 sheet.setDefaultRowHeight((short) (3 * 256)); sheet.setDefaultColumnWidth(25); // 添加excel第一行表头信息(你想要添加的表头数据,集合类型,遍历放进去) for (int i = 0; i < fieldList.size(); i++) { // 创建一个单元格 HSSFCell cell = row.createCell(i); // 设置单元格的样式,工具类在下面代码中会有放 cell.setCellStyle(excelUtil.getRow1CellStyle(workbook)); // 将数据放入excel的单元格中  HSSFRichTextString text = new HSSFRichTextString(String.valueOf(fieldList.get(i).getFieldName())); cell.setCellValue(text); } // 开始创建excel单元格数据,从第二行开始(excel下标是1) int rowNum = 1; // 添加excel行数据的集合(你自己的数据集合遍历) for (Map<String, Object> map : resultSetResp.getList()) { // 创建一个单元格 HSSFRow row1 = sheet.createRow(rowNum); // 设置行的高度 row1.setHeightInPoints(20); // 遍历你的表头数据,根据表头的数据获取对应的数据(我的数据是map的key,value形式) for (int i = 0; i < fieldList.size(); i++) { // 放入单元格中 row1.createCell(i).setCellValue(String.valueOf(map.get(fieldList.get(i).getFieldName()))); } // 加一行,继续循环添加 rowNum++; } // 工具类导出excel excelUtil.downLoadExcel(fileName, response, request, workbook); 
4、excelUtil工具类
(1)导出时设置浏览器的相应格式,请注意,经过我的多次尝试,发现这种写法通过postman测试会出现文件名缺失或者乱码的问题,但是前端人员可以通过代码调通;还有一种写法是我自己postman测试没问题,前段人员调的时候乱码,大家可以自行尝试,代码一并放下面。
// postman测试文件名乱码,前段可以用形式 public void downLoadExcel(String fileName, HttpServletResponse response, HttpServletRequest request, Workbook workbook) throws IOException { ServletOutputStream outputStream = null; try { try { // 设置文件名的编码格式 fileName = new String(fileName.getBytes(), "ISO8859-1"); } catch (UnsupportedEncodingException e) { log.info("导出失败 --0"); } // 设置文件的编码格式 response.setContentType("application/octet-stream;charset=ISO8859-1"); // 设置响应头添加附件 response.setHeader("Content-Disposition", "attachment;filename=" + fileName); // 设置不需要缓存 response.addHeader("Pargam", "no-cache"); response.addHeader("Cache-Control", "no-cache"); response.addHeader("fileName",fileName); outputStream = response.getOutputStream(); workbook.write(outputStream); outputStream.flush(); } catch (IOException e) { log.error("导出excel时出错:{}", e); } finally { workbook.close(); if (outputStream != null) { outputStream.flush(); outputStream.close(); } } } // 自测无乱码的相应格式,前段调用文件名乱码(大家可以自行调试) /*
		fileName = URLEncoder.encode(fileName, "utf-8");
        response.setHeader("content-Type", "application/vnd.ms-excel");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ";" + "filename*=utf-8''" + fileName);

        fileName = new String(fileName.getBytes("utf-8"), "ISO_8859_1");

        response.setHeader("fileName", fileName);
        response.setCharacterEncoding("utf-8");
        */ 
(2)设置单元格的样式
public CellStyle getRow1CellStyle(Workbook workbook) { CellStyle cellStyle = workbook.createCellStyle(); //设置水平居中 cellStyle.setAlignment(HorizontalAlignment.CENTER); //设置垂直居中 cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); //设置下边框 cellStyle.setBorderBottom(BorderStyle.THIN); //设置上边框 cellStyle.setBorderTop(BorderStyle.THIN); //设置走边框 cellStyle.setBorderLeft(BorderStyle.THIN); //设置右边框 cellStyle.setBorderRight(BorderStyle.THIN); //设置字体 Font font = workbook.createFont(); //设置字号 font.setFontHeightInPoints((short) 14); //设置是否为斜体 font.setItalic(false); //设置是否加粗 font.setBold(false); //设置字体颜色 font.setColor(IndexedColors.BLACK.index); cellStyle.setFont(font); //设置背景 cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); cellStyle.setFillForegroundColor(IndexedColors.YELLOW.index); return cellStyle; } 
5、效果(数据居中我没实现成功,大家有实现的可以留言哦)

本文地址:https://blog.csdn.net/weixin_42322400/article/details/108018259

《springBoot中使用easypoi(非注解式)导出excel记录.doc》

下载本文的Word格式文档,以方便收藏与打印。