【Spring系列】SpringBoot+Mybatis实现excel文件的下载方式

SpringBoot整合POI实现xls文件下载

使用文件流的方式进行返回

引入依赖

<dependency>
	<groupId>org.apache.poi</groupId>
	<artifactId>poi</artifactId>
	<version>3.10-FINAL</version>
</dependency>
<dependency>
	<groupId>org.apache.poi</groupId>
	<artifactId>poi-ooxml</artifactId>
	<version>3.10-FINAL</version>
</dependency>
<dependency>
 	<groupId>org.apache.poi</groupId>
	<artifactId>poi-ooxml-schemas</artifactId>
	<version>3.10-FINAL</version>
</dependency>

Controller层代码

	@RequestMapping("/selectUserListToExcel")
    public void selectUserListToExcel(HttpServletResponse response){

        try {
            userService.exportExcelFile(response);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

Service层代码

public void exportExcelFile(HttpServletResponse response) throws IOException {
        // 查询所有用户信息
        List<UserEntity> userEntities = userEntityMapper.queryUserList("");

        String xlsFile_name = "userInfos" + ".xlsx";

        String[] title = {"username", "password", "start_time", "stop_time"};
        String[][] values = new String[userEntities.size()][4];

        for(int i = 0;i < userEntities.size(); i++) {

            values[i][0] = userEntities.get(i).getUsername();
            values[i][1] = userEntities.get(i).getPassword();

            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-ss HH:mm:ss");

            values[i][2] = dateFormat.format(userEntities.get(i).getStartTime());
            values[i][3] = dateFormat.format(userEntities.get(i).getStopTime());
        }
        
        // 生成表格
        SXSSFWorkbook wb = new SXSSFWorkbook();

        // 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet
        SXSSFSheet sheet = (SXSSFSheet) wb.createSheet("sheet1");

        // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
        SXSSFRow row = (SXSSFRow) sheet.createRow(0);

        // 第四步,创建单元格,并设置值表头 设置表头居中
        CellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

        //声明列对象
        SXSSFCell cell = null;

        //创建标题
        for(int i=0;i<title.length;i++){
            cell = (SXSSFCell) row.createCell(i);
            cell.setCellValue(title[i]);
            cell.setCellStyle(style);
        }

        //创建内容
        for(int i=0;i<values.length;i++){
            row = (SXSSFRow) sheet.createRow(i + 1);
            for(int j=0;j<values[i].length;j++){
                //将内容按顺序赋给对应的列对象
                row.createCell(j).setCellValue(values[i][j]);
            }
        }
        
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" + xlsFile_name);
        response.flushBuffer();
        OutputStream outputStream = response.getOutputStream();
        wb.write(response.getOutputStream());
        outputStream.flush();
        outputStream.close();
    }

这段代码主要是在数据库中查询出所有的用户信息,存入List,可以根据自己需求修改表格中的内容,这种方法主要还是借用HttpServletResponsePOI的SXSSFWorkbook的工作流进行返回

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值