java导出数据到Excel文件 前端进行下载

通通是干货,直接贴代码

Maven依赖

        
            org.apache.poi
            poi
            3.17
        
        
            org.apache.poi
            poi-ooxml
            3.17
        
        
            org.apache.poi
            poi-ooxml-schemas
            4.0.1
        
    /**
     * 导出宿舍人员信息 Excel文件
     * @param type
     * @param id
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    @GetMapping("/download/export/student")
    public R setUpExcel(@Param("type") String type, @Param("id") String id, HttpServletRequest request, HttpServletResponse response) throws IOException{
        List<RoomPersonnel> roomPersonnelList=roomPersonnelService.getRoomStudent(type, id,LyraUtils.getCompanyId());
     /*   if (ObjectUtil.isNull(roomPersonnelList)||roomPersonnelList.isEmpty()){
            return R.failed("无数据导出");
        }*/
        // 第一步,创建一个webbook,对应一个Excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet("学生信息");
        // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
        HSSFRow row = sheet.createRow((int) 0);
        // 第四步,创建单元格,并设置值表头 设置表头居中
       // HSSFCellStyle style = wb.createCellStyle();

        wb.setSheetName(0, "宿舍学生信息");
        String[] headers = { "学生姓名", "学号","性别","年级","班级","宿舍楼","宿舍号","床位号","入住时间"};
        for (short i = 0; i < headers.length; i++) {
            //创建单元格,每行多少数据就创建多少个单元格
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            //给单元格设置内容
            cell.setCellValue(text);
        }
        // 第五步,写入实体数据 这些数据从数据库得到,
        HSSFRow rows;
        HSSFCell cells;
        for (int i = 0; i < roomPersonnelList.size(); i++) {
            // 第三步:在这个sheet页里创建一行
            rows = sheet.createRow(i+1);
            // 第四步:在该行创建一个单元格
            cells = rows.createCell(0);
            // 第五步:在该单元格里设置值
            cells.setCellValue(roomPersonnelList.get(i).getStuName());
            cells = rows.createCell(1);
            cells.setCellValue(roomPersonnelList.get(i).getStudentId());
            cells = rows.createCell(2);
            cells.setCellValue(roomPersonnelList.get(i).getGender());
            cells = rows.createCell(3);
            cells.setCellValue(roomPersonnelList.get(i).getGradeName());
            cells = rows.createCell(4);
            cells.setCellValue(roomPersonnelList.get(i).getClassName());
            cells = rows.createCell(5);
            cells.setCellValue(roomPersonnelList.get(i).getDormId());
            cells = rows.createCell(6);
            cells.setCellValue(roomPersonnelList.get(i).getRoomId());
            cells = rows.createCell(7);
            cells.setCellValue(roomPersonnelList.get(i).getBunkId());
            cells = rows.createCell(8);
            cells.setCellValue(roomPersonnelList.get(i).getCreateTime());
        }
        try {
            String fileName = "宿舍人员信息.xls";// 文件名
            response.setContentType("application/x-msdownload");
            response.setHeader("Content-Disposition", "attachment; filename="
                    + URLEncoder.encode(fileName, "UTF-8"));
            OutputStream out = response.getOutputStream();
            wb.write(out);
            wb.close();
        } catch (Exception e) {
            e.printStackTrace();
        } /*finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }*/
        return null;
    }
  • 普通—前端访问后端路径导出数据*
window.location.href = "/school/roomPersonnel/download/export/student?type="+type +"&id="+id;
  • 点击导出然后给页面加屏障----直到数据导出成功取消屏障*
			//导出框提交事件
			exportStudentSave: function(row, done) {
				let ids = ''
				if (this.exportStudentForm.type === 1) {
					ids = this.exportStudentForm.dormId
				} else if (this.exportStudentForm.type === 2) {
					ids = this.exportStudentForm.gradeId
				} else if (this.exportStudentForm.type === 3) {
					ids = this.exportStudentForm.classId
				}
				let par = {
					type:this.exportStudentForm.type,
					id:ids,
                    companyId:this.userInfo.companyId
				}
                const loading = this.$loading({
                    lock: true,
                    text: '正在导出宿舍人员,请稍等',
                    spinner: 'el-icon-loading',
                    background: 'rgba(0, 0, 0, 0.7)'
                });
                exportStudent(par).then(res => {
                    loading.close();
                    const blob = new Blob([res.data]);
                    const fileName = '宿舍人员信息.xls';
                    const linkNode = document.createElement('a');

                    linkNode.download = fileName; //a标签的download属性规定下载文件的名称
                    linkNode.style.display = 'none';
                    linkNode.href = URL.createObjectURL(blob); //生成一个Blob URL
                    document.body.appendChild(linkNode);
                    linkNode.click();  //模拟在按钮上的一次鼠标单击

                    URL.revokeObjectURL(linkNode.href); // 释放URL 对象
                    document.body.removeChild(linkNode);
                    });
				this.exportStudentVisible = false
				done()
			},

你可能感兴趣的:(java,下载,导出excel)