POI+VUE 导出Excle

VUE+POI 导出Excle

引入POI相关依赖

// 本次项目所用3.17版本
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.17</version>
        </dependency>

Controller部分

// An highlighted block
    @ApiOperation(value = "导出" ,notes = "导出所有")
    @RequestMapping("ExportTeam")
    public void exportAllTeam(HttpServletRequest request, HttpServletResponse response){
     
           fdDoctorTeamService.exportDocotTeam(request,response);
    }

Service部分

// An highlighted block
@Override
    public void exportDocotTeam(HttpServletRequest request, HttpServletResponse response) {
     
        try{
     
            Long orgId = OperContextUtil.getCurrentOrgId();
            String teamNameLike="";
            List<FdDoctorTeamDto> list = fdDoctorTeamMapper.listDto(orgId, teamNameLike);
            translateService.translate(list);
            //-------------以上为获取后台数据-------------------
            //创建Excel工作表
            HSSFWorkbook wb=new HSSFWorkbook();
            //设置工作表名称
            HSSFSheet sheet = wb.createSheet("工作表名称");
            // 设置行
            HSSFRow row = null;
            //设置列下标
            int columnIndex = 0;
   
            row = sheet.createRow(0);
            row.setHeight((short) (12.50 * 20));//设置行高
            HSSFFont font = wb.createFont();//创建字体样式
            HSSFCellStyle style = wb.createCellStyle();//创建样式
            //---设置表头内容
            HSSFCell cell=row.createCell(0);
            cell.setCellValue("名称");
            HSSFCell cell1=row.createCell(1);
            cell1.setCellValue("性别");
            HSSFCell cell2=row.createCell(2);
            cell2.setCellValue("联系电话");
            HSSFCell cell3=row.createCell(3);
            cell3.setCellValue("单位机构");
            //设置表头样式
            style.cloneStyleFrom(cell.getCellStyle());
            style.setWrapText(true);
            style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            style.setFillForegroundColor(IndexedColors.PALE_BLUE.index);
            font.setFontHeightInPoints((short)10);//设置字号
            font.setColor(IndexedColors.BLACK.index);//设置表头背景颜色
            font.setBold(true);
            style.setFont(font);
            style.setVerticalAlignment(VerticalAlignment.CENTER);//设置字体垂直居中
            style.setBorderBottom(BorderStyle.THIN);//下边框
            style.setBorderLeft(BorderStyle.THIN);//左边框
            style.setBorderRight(BorderStyle.THIN);//右边框
            style.setBorderTop(BorderStyle.THIN); //上边框
            //将style样式赋值给每个表头单元格
            cell.setCellStyle(style);
            cell1.setCellStyle(style);
            cell2.setCellStyle(style);
            cell3.setCellStyle(style);
            //表主体内容
            for (int i = 0; i < list.size(); i++) {
     
                row = sheet.createRow(i+1);
                FdDoctorTeamDto entity = list.get(i);
                columnIndex = 0;
                row.createCell(columnIndex).setCellValue(entity.getTeamName());
                row.createCell(++columnIndex).setCellValue(entity.getEmpName());
                row.createCell(++columnIndex).setCellValue(entity.getContactPhone());
                row.createCell(++columnIndex).setCellValue(entity.getOrgName());
            }
            sheet.setDefaultRowHeight((short) (16.5 * 20));
            //列宽自适应
            for (int i = 0; i <= 3; i++) {
     
                sheet.autoSizeColumn(i);
            }


            String title= "Team";
            response.setContentType("application/octet-stream;charset=utf-8");
            response.setHeader("Content-disposition", "attachment;fileName=" + title + ".xlsx");

            OutputStream ouputStream = response.getOutputStream();
            wb.write(ouputStream);
            ouputStream.flush();
            ouputStream.close();
        }catch (Exception e){
     
            e.printStackTrace();
        }
        }

Vue 部分

			// 按钮样式
            <el-button
              type="warning"
              icon="el-icon-download"
              size="mini"
              @click="handleExport" 
              v-hasPermi="['kcgl:bloodinfo:export']">导出</el-button>
          //JS
            methods: {
     
          handleExport() {
     
            this.$confirm('是否确认导出所有信息?', "警告", {
     
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
                }).then(()=> {
          
                    var token =window.localStorage.getItem("token");
                    var form = document.createElement("form");
                    form.style.display = "none";
                    form.action =this.config.fdUrl+'/fdDoctorTeam/ExportTeam?token='+token;//请求路径
                    form.method = "post";
                    document.body.appendChild(form);
                    form.submit();
                    form.remove();
                    this.$message({
     
                    message: '导出成功',
                    type: 'success'
                }).then(response =>{
     

                }).catch(function() {
     });
                });
            },
            }

你可能感兴趣的:(poi,java,vue.js)