Java+VUE 导出Excel(复制代码,粘贴即用)

导出模拟练习成绩(样板图)
在这里插入图片描述

java后台方法:(复制代码,粘贴即用)

 try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddss");
            Date date = new Date();
            OutputStream out = null;
            out = response.getOutputStream();
            response.setContentType("application/msexcel");
            // 下载文件能正常显示中文
            response.setHeader(
                    "Content-Disposition",
                    "attachment;filename=" + URLEncoder.encode(sdf.format(date)+".xls", "UTF8"));
            response.setHeader("FileName", URLEncoder.encode(sdf.format(date)+".xls", "UTF8") + "");//导出的Excel文件名称
            response.setHeader("Access-Control-Expose-Headers", "FileName");

            List< Map<String,Object> > allsource = stuscoreMapper.getAllStuScore(params);//数据查询--需要导出的数据

            String sheetName="考生成绩表" ;//设置sheet名称
            String excelType = "2003" ;
            List< String > headList = new ArrayList<>();
            //列标题设置
            String c0= "姓名";
            String c1= "性别";
            String c2 = "年龄";
            String c3 = "身份证";
            String c4 = "准考证";
            String c5 = "工种";
            String c6 = "级别";
            String c7 = "职业道德";
            String c8 = "专业理论";
            String c9 = "技能实操";
            String c10 = "总分";
            String c11 = "职业道德标记";
            String c12 = "专业理论标记";
            String c13 = "技能实操标记";
            String c14 = "考核结果";
            headList.add(c0);
            headList.add(c1);
            headList.add(c2);
            headList.add(c3);
            headList.add(c4);
            headList.add(c5);
            headList.add(c6);
            headList.add(c7);
            headList.add(c8);
            headList.add(c9);
            headList.add(c10);
            headList.add(c11);
            headList.add(c12);
            headList.add(c13);
            headList.add(c14);

            Workbook wb = null;
            /*创建文件*/
            if (excelType == null || excelType.endsWith("2003")) {
                /*操作Excel2003以前(包括2003)的版本,扩展名是.xls */
                wb = new HSSFWorkbook();
            } else if (excelType.endsWith("2007")) {
                /*XSSFWorkbook:是操作Excel2007的版本,扩展名是.xlsx */
                wb = new XSSFWorkbook();
            } else {   //默认为2003版本
                /*操作Excel2003以前(包括2003)的版本,扩展名是.xls */
                wb = new HSSFWorkbook();
            }
            /*Excel文件创建完毕*/
            CreationHelper createHelper = wb.getCreationHelper();  //创建帮助工具
            /*创建表单*/
            Sheet sheet = wb.createSheet(sheetName != null ? sheetName : "new sheet");
            //设置表头字体
            Font headFont = wb.createFont();
            headFont.setFontHeightInPoints((short) 14); // 字体大小
            headFont.setFontName("仿宋 常规");// 字体样式
            headFont.setItalic(false);
            headFont.setStrikeout(false);

            //设置表头单元格样式
            CellStyle headStyle = wb.createCellStyle();
            headStyle.setBorderBottom(BorderStyle.THIN);  //设置单元格线条
            // headStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());   //设置单元格颜色
            headStyle.setBorderLeft(BorderStyle.THIN);
            headStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
            headStyle.setBorderRight(BorderStyle.THIN);
            headStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
            headStyle.setBorderTop(BorderStyle.THIN);
            headStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
            headStyle.setAlignment(HorizontalAlignment.CENTER);    //设置水平对齐方式
            headStyle.setVerticalAlignment(VerticalAlignment.CENTER);  //设置垂直对齐方式
            headStyle.setShrinkToFit(true);  //自动伸缩
            headStyle.setFont(headFont);  //设置字体

            /*设置数据单元格格式*/
            Font dataFont = wb.createFont();
            dataFont.setFontHeightInPoints((short) 12);
            dataFont.setFontName("仿宋 常规");
            dataFont.setItalic(false);
            dataFont.setStrikeout(false);

            CellStyle dataStyle = wb.createCellStyle();
            dataStyle.setBorderBottom(BorderStyle.THIN);  //设置单元格线条
            dataStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());   //设置单元格颜色
            dataStyle.setBorderLeft(BorderStyle.THIN);
            dataStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
            dataStyle.setBorderRight(BorderStyle.THIN);
            dataStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
            dataStyle.setBorderTop(BorderStyle.THIN);
            dataStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
            dataStyle.setAlignment(HorizontalAlignment.CENTER);    //设置水平对齐方式
            dataStyle.setVerticalAlignment(VerticalAlignment.CENTER);  //设置垂直对齐方式
            //dataStyle.setShrinkToFit(true);  //自动伸缩
            dataStyle.setFont(dataFont);// 字体样式
            /*创建行Rows及单元格Cells*/
            Row headRow = sheet.createRow(0); //第一行为头
            for (int i = 0; i < headList.size(); i++) {  //遍历表头数据
                Cell cell = headRow.createCell(i);  //创建单元格
                cell.setCellValue(createHelper.createRichTextString(headList.get(i)));  //设置值
                cell.setCellStyle(headStyle);  //设置样式
            }
            int rowIndex = 1;  //当前行索引
            for (int j = 0; j < allsource.size(); j++) {  //编译每一行
                Row row = sheet.createRow(rowIndex++); //第一行为头
                //                System.err.println(allsource.get(j));
                //                UserTest userTest = resultmap.get(j);
                Cell cell = row.createCell(0);

                cell.setCellValue(createHelper.createRichTextString(allsource.get(j).get("name").toString()));//
                cell.setCellStyle(dataStyle);
                cell = row.createCell(1);
                cell.setCellValue(createHelper.createRichTextString(allsource.get(j)
                        .get("sex")==null?"":allsource.get(j).get("sex").toString()));//
                cell.setCellStyle(dataStyle);
                cell = row.createCell(2);
                cell.setCellValue(createHelper.createRichTextString(allsource.get(j)
                        .get("age")==null?"":allsource.get(j).get("age").toString()));//
                cell.setCellStyle(dataStyle);
                cell = row.createCell(3);
                cell.setCellValue(createHelper.createRichTextString(allsource.get(j)
                        .get("idNumber")==null?"":allsource.get(j).get("idNumber").toString()));//
                cell.setCellStyle(dataStyle);
                cell = row.createCell(4);
                cell.setCellValue(createHelper.createRichTextString(allsource.get(j)
                        .get("examnumber")==null?"":allsource.get(j).get("examnumber").toString()));//
                cell.setCellStyle(dataStyle);;
                cell = row.createCell(5);
                cell.setCellValue(createHelper.createRichTextString(allsource.get(j)
                        .get("worktype")==null?"":allsource.get(j).get("worktype").toString()));//
                cell.setCellStyle(dataStyle);
                cell = row.createCell(6);
                cell.setCellValue(createHelper.createRichTextString(allsource.get(j)
                        .get("grade")==null?"":allsource.get(j).get("grade").toString()));//
                cell.setCellStyle(dataStyle);
                cell = row.createCell(7);
                cell.setCellValue(createHelper.createRichTextString(allsource.get(j)
                        .get("zyddscore")==null?"":allsource.get(j).get("zyddscore").toString()));//
                cell.setCellStyle(dataStyle);
                cell = row.createCell(8);
                cell.setCellValue(createHelper.createRichTextString(allsource.get(j)
                        .get("zyllscore")==null?"":allsource.get(j).get("zyllscore").toString()));//
                cell.setCellStyle(dataStyle);
                cell = row.createCell(9);
                cell.setCellValue(createHelper.createRichTextString(allsource.get(j)
                        .get("jnscscore")==null?"":allsource.get(j).get("jnscscore").toString()));//
                cell.setCellStyle(dataStyle);
                cell = row.createCell(10);
                cell.setCellValue(createHelper.createRichTextString(allsource.get(j)
                        .get("totalscore")==null?"":allsource.get(j).get("totalscore").toString()));//
                cell.setCellStyle(dataStyle);
                cell = row.createCell(11);
                cell.setCellValue(createHelper.createRichTextString(allsource.get(j)
                        .get("zyddwj")==null?"":allsource.get(j).get("zyddwj").toString()));//
                cell.setCellStyle(dataStyle);
                cell = row.createCell(12);
                cell.setCellValue(createHelper.createRichTextString(allsource.get(j)
                        .get("zyllwj")==null?"":allsource.get(j).get("zyllwj").toString()));//
                cell.setCellStyle(dataStyle);
                cell = row.createCell(13);
                cell.setCellValue(createHelper.createRichTextString(allsource.get(j)
                        .get("jncswj")==null?"":allsource.get(j).get("jncswj").toString()));//
                cell.setCellStyle(dataStyle);
                cell = row.createCell(14);
                cell.setCellValue(createHelper.createRichTextString(
                        allsource.get(j) .get("result")==null?"":allsource.get(j).get("result").toString()));//
                cell.setCellStyle(dataStyle);

//                cell = row.createCell(4);
//                cell.setCellValue(createHelper.createRichTextString(allsource.get(j)
//                        .get("percount")==null?"":allsource.get(j).get("percount").toString()));//
            }
            /*创建rows和cells完毕*/
            /*设置列自动对齐*/
            for (int i = 0; i < headList.size(); i++) {
                sheet.autoSizeColumn(i);
            }
            wb.write(out);   //将workbook写入文件流
            wb.close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

前台代码块

/**
     * 导出成绩EXCEL
     */
    outputScore() {
      let params = {

      }
      createStuscoreExcle(params).then(res => {
        if (res.data.size != 0) {
          const blob = new Blob([res.data]);
          const fileName = res.headers.filename;
          if ('download' in document.createElement("a")) {
            const link = document.createElement("a");
            link.download = fileName;
            link.style.display = 'none';
            link.href = URL.createObjectURL(blob);
            document.body.appendChild(link);
            link.click();
            URL.revokeObjectURL(link.href);
            document.body.removeChild(link);
          } else {
            navigator.msSaveBlob(blob, fileName);
          }
        } else {
          this.$message({
            type: 'error',
            message: "导出失败,请重试",
            center: true,
            offset: 400
          });
          
        }
        
      })
      
    },

请求

export function createStuscoreExcle(params) {
  return request({
    url: "scoreman/createStuscoreExcle",
    method: 'post',
    responseType: 'blob',
    data: params,
  })
}
Logo

前往低代码交流专区

更多推荐