您现在的位置是: 首页> java > 正文

java统计分析数据Excel导出

时间: 2020-12-07 00:32:27 来源:网络 作者:自由 分类:java
简介:

大家在做项目开发的时候,经常需要统计数据导出excel,一般统计数据的时候是用EChart来做统计显示用,导出表格统计数据一般用poi来导出,前台展示咱们就先不讲了,对于后台的excel导出,咱们今天来看一下,希望能够帮助大家,下面废话就不多说了,开始搞代码。

1.spring boot中Controller代码

 /**
     * 导出数据
     *
     * @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前台调用

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

标签:java

文章声明
版权声明:本文为作者原创,仅用于本站访客学习、研究和交流目的,未经授权禁止转载
联系 作者

一个90后草根站长!13年入行。一直潜心研究技术,一边工作一边积累经验,分享一些个人后端技术(java、python、c#、php等),以及前端相关等心得。