spring Boot+vue做导出excel功能

最近在写后台管理系统,要实现后台数据导出excel这个功能,后台用的是spring boot,前台用的是vue+iview

maven依赖:

       <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.15</version>
            <scope>compile</scope>
        </dependency>

工具类:

public class FileUtil {
    public static void createFile(HttpServletResponse response, HSSFWorkbook workbook) {
        // 设置文件名
        String fileName ="数据信息";
        try {
            // 捕获内存缓冲区的数据,转换成字节数组
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            workbook.write(out);
            // 获取内存缓冲中的数据
            byte[] content = out.toByteArray();
            // 将字节数组转化为输入流
            InputStream in = new ByteArrayInputStream(content);
            //通过调用reset()方法可以重新定位。
            response.reset();
            // 如果文件名是英文名不需要加编码格式,如果是中文名需要添加"iso-8859-1"防止乱码
            response.setHeader("Content-Disposition", "attachment; filename=" + new String((fileName + ".xls").getBytes(), "iso-8859-1"));
            response.addHeader("Content-Length", "" + content.length);
            response.setContentType("application/vnd.ms-excel;charset=UTF-8");
            ServletOutputStream outputStream = response.getOutputStream();
            BufferedInputStream bis = new BufferedInputStream(in);
            BufferedOutputStream bos = new BufferedOutputStream(outputStream);
            byte[] buff = new byte[8192];
            int bytesRead;
            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                bos.write(buff, 0, bytesRead);
            }
            bis.close();
            bos.close();
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}


service层接口:

    RestResult getExcel(HttpServletRequest request, HttpServletResponse response, String keyword, Integer kind, Integer type, Integer status);

实现类:

    public RestResult getExcel(HttpServletRequest request, HttpServletResponse response, String keyword, Integer kind, Integer type, Integer status) {
        List<WithdrawVo> withdrawVos = withDrawRecordMapper.getExcel(keyword, kind, type, status);

        // 创建工作簿
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 创建表
        HSSFSheet sheet = workbook.createSheet("提现信息");
        // 创建行
        HSSFRow row = sheet.createRow(0);
        // 创建单元格样式
        HSSFCellStyle cellStyle = workbook.createCellStyle();
        // 表头
        String[] head = {"账号", "昵称", "提现种类", "提现类型", "申请状态", "银行卡号", "提现金额", "性别",
                "自身返利余额", "推广分销余额", "创建时间"};
        HSSFCell cell;
        // 设置表头
        for (int iHead = 0; iHead < head.length; iHead++) {
            cell = row.createCell(iHead);
            cell.setCellValue(head[iHead]);
            cell.setCellStyle(cellStyle);
        }
        // 设置表格内容
        for (int iBody = 0; iBody < withdrawVos.size(); iBody++) {
            row = sheet.createRow(iBody + 1);
            WithdrawVo u = withdrawVos.get(iBody);
            String[] userArray = new String[11];
            userArray[0] = u.getAccount();
            userArray[1] = u.getNickname();
            String sex=(u.getSex() ==0 ?"保密":(u.getSex()==1?"男" : "女"));
            String kinds=u.getKind()==1? "自返奖励":"推广基金";
            userArray[2] = kinds;
            String cardType=u.getType()==1? "微信":"银行卡";
            userArray[3] = cardType;
            String statuss=(u.getStatus() ==0 ?"待审核":(u.getStatus()==1?"审核通过" : "审核失败"));
            userArray[4] = statuss;
            userArray[5] = u.getBankCard() + "";
            userArray[6] = u.getMoney() + "";
            userArray[7] = sex;
            userArray[8] = u.getRebateBalance() + "";
            userArray[9] = u.getSpreadBalance() + "";
            Date d1=u.getCreateTime();
            String s1=String.format("%tY-%tm-%td %tH:%tM:%tS",d1,d1,d1,d1,d1,d1);
            userArray[10] = s1 ;
            for (int iArray = 0; iArray < userArray.length; iArray++) {
                row.createCell(iArray).setCellValue(userArray[iArray]);
            }
        }
        // 生成Excel文件
        FileUtil.createFile(response, workbook);
        return null;
    }

controller层:

    @RequestMapping(value="/excel",method = RequestMethod.GET)
        public RestResult exportExcel(HttpServletRequest request, HttpServletResponse response ){
    
        String keyword = request.getParameter("keyword");
        Integer kind=Integer.valueOf(request.getParameter("kind"));
        Integer type=Integer.valueOf(request.getParameter("type"));
        Integer status=Integer.valueOf(request.getParameter("status"));
        return withdrawService.getExcel(request,response,keyword,kind,type,status);
    }

结果如下:

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43481793/article/details/89517720
今日推荐