前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java统计分析数据Excel导出

java统计分析数据Excel导出

作者头像
十分钟空间
发布2022-08-17 14:02:49
7400
发布2022-08-17 14:02:49
举报
文章被收录于专栏:Springboot框架学习

1.spring boot中Controller代码

代码语言:javascript
复制
 /**
     * 导出数据
     *
     * @param request
     * @param response
     * @throws IOException
     */
    @PostMapping("/ExportUserInfo")
    public void ExportUserInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //表头数据
        String[] header = {"姓名", "编号", "年龄", "地址"};

        //声明一个工作簿
        HSSFWorkbook workbook = new HSSFWorkbook();

        //生成一个表格,设置表格名称为"学生表"
        HSSFSheet sheet = workbook.createSheet("用户信息");

        //设置表格列宽度为10个字节
        sheet.setDefaultColumnWidth(10);
        //创建标题的显示样式
        HSSFCellStyle headerStyle = workbook.createCellStyle();
        headerStyle.setFillForegroundColor(IndexedColors.YELLOW.index);
        headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        //创建第一行表头
        HSSFRow headrow = sheet.createRow(0);

        //遍历添加表头(下面模拟遍历学生,也是同样的操作过程)
        for (int i = 0; i < header.length; i++) {
            //创建一个单元格
            HSSFCell cell = headrow.createCell(i);
            //创建一个内容对象
            HSSFRichTextString text = new HSSFRichTextString(header[i]);

            //将内容对象的文字内容写入到单元格中
            cell.setCellValue(text);
            cell.setCellStyle(headerStyle);
        }

        //获取导出的数据
        SysUserPageInvo invo = new SysUserPageInvo();
        List<SysUserOut> list = this.sysUserService.getUserList(invo);

    // 设置样式
        HSSFCellStyle bodyStyle = workbook.createCellStyle();
        bodyStyle.setBorderTop(BorderStyle.THIN);
        bodyStyle.setBorderBottom(BorderStyle.THIN);
        bodyStyle.setBorderLeft(BorderStyle.THIN);
        bodyStyle.setBorderRight(BorderStyle.THIN);
    
        for (int i = 0; i < list.size(); i++) {
            //创建一行
            HSSFRow row1 = sheet.createRow(i + 1);
            //姓名
            Cell cell0 = row1.createCell(0);
            cell0.setCellStyle(bodyStyle);
            cell0.setCellValue(new HSSFRichTextString(list.get(i).getUserName()));
            //编号
            Cell cell3 = row1.createCell(1);
            cell3.setCellStyle(bodyStyle);
            cell3.setCellValue(new HSSFRichTextString(list.get(i).getTeamType().toString()));
            //年龄
            Cell cell4 = row1.createCell(2);
            cell4.setCellStyle(bodyStyle);
            cell4.setCellValue(new HSSFRichTextString(list.get(i).getAccountNumbers().toString()));
            //地址
            Cell cell6 = row1.createCell(3);
            cell6.setCellStyle(bodyStyle);
            cell6.setCellValue(new HSSFRichTextString(list.get(i).getWelfarelables()));


        }

        //准备将Excel的输出流通过response输出到页面下载
        //八进制输出流
        response.setContentType("application/octet-stream;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");//设置返回数据编码
        //这后面可以设置导出Excel的名称,此例中名为student.xls
        response.setHeader("Content-disposition", "attachment;filename=用户信息.xls");

        //刷新缓冲
        response.flushBuffer();

        //workbook将Excel写入到response的输出流中,供页面下载
        workbook.write(response.getOutputStream());
    }

2. element ui前台调用

代码语言:javascript
复制
   ExportUserInfo() {
        this.$axios({
          method: 'POST',
          url: this.url.ExportUserInfo,
          responseType: 'blob'
        }).then(response => {
          if (!response) {
            return
          }
          const blob = new Blob([response.data])
          if (window.navigator && window.navigator.msSaveOrOpenBlob) {
            navigator.msSaveBlob(blob, '用户信息.xls')
          } else {
            let u = window.URL.createObjectURL(response.data)
            let aLink = document.createElement('a')
            aLink.style.display = 'none'
            aLink.href = u
            aLink.setAttribute('download', '用户信息.xls')
            document.body.appendChild(aLink)
            aLink.click()
            document.body.removeChild(aLink)
            window.URL.revokeObjectURL(u)
          }
        }).catch(error => {
          throw error
        })
      },

          java使用poi导出excel只要把数据组织好,按照固定的格式输出就可以,需要注意的是导出的格式如果是不是中规中矩的格式(第一行标题,第二行以下是数据)的话,需要用模版或者根据单元格位置固定导出,大家可以把这个方法抽成一个共同的方法方便以后开发项目的时候继续使用。后续我会分享更多的技术相关的内容,请大家多多关注。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/12/07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.spring boot中Controller代码
  • 2. element ui前台调用
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档