SSM实现Excel的后台导出

1.controller层的写法。本文档以导出考试试题为例。

@ApiOperation("导出")
	@PostMapping(value = "/exportQuestions")
	public Object exportQuestions(String examQuestions) throws IOException, ParseException {
		Object result = excelExportService.ExcelExport(examQuestions);
		return result;
	}

2.service层写法。

public Object ExcelExport(String examQuestions ) throws FileNotFoundException, IOException {
    //根据自己的逻辑处理,随意取名
	 String examQuestionsName = "";
	//new一个新的文件
     File file = new File("C:/驾培考试考题导出"); 
     if (!file.exists()) {
     //如果文件名不存在就创建一个
       file.createNewFile();
    }   
     try {
    	 //在本地生成Excel文件,根据提醒取名
         workbook = new XSSFWorkbook();
         FileOutputStream fileOut = new FileOutputStream(file.getPath()+"/"+examQuestionsName+".xlsx");
         workbook.write(fileOut);
         fileOut.close();
     } catch (IOException e) {
             e.printStackTrace();
         }
     //查询出考题信息
	 ExamQuestionsInfoQueryParam examQuestionsInfoQueryParam = new ExamQuestionsInfoQueryParam();
	 examQuestionsInfoQueryParam.setPs(Integer.MAX_VALUE);
	 examQuestionsInfoQueryParam.setTypeNo(3);
	 examQuestionsInfoQueryParam.setExamQuestions(examQuestions);
	 List<ExamQuestionsInfo> examQuestionsInfos =   examQuestionsInfoMapper.listQuery(examQuestionsInfoQueryParam);
	
	 wk = new XSSFWorkbook();
	
        // 创建一张工作表
        XSSFSheet sheet = wk.createSheet();
        // 设置宽度
        sheet.setColumnWidth(0, 5000);
        XSSFRow row = sheet.createRow(0);
        // 创建第一行的第一个单元格
        // 想单元格写值
        XSSFCell cell = row.createCell((short) 0);
        cell.setCellValue("序号");
        cell=row.createCell((short)1);
        cell.setCellValue("题干");
        cell=row.createCell((short)2);
        cell.setCellValue("题型");
        cell=row.createCell((short)3);
        cell.setCellValue("A");
        cell=row.createCell((short)4);
        cell.setCellValue("B");
        cell=row.createCell((short)5);
        cell.setCellValue("C");
        cell=row.createCell((short)6);
        cell.setCellValue("D");
        cell=row.createCell((short)7);
        cell.setCellValue("答案");
        //遍历考题信息开始写入
        for(short i = 0 ;i <examQuestionsInfos.size() ;i++) {
        	row = sheet.createRow(i+1);
        	row.createCell(0).setCellValue(i+1);
        	row.createCell(1).setCellValue(examQuestionsInfos.get(i).getExamQuestionsContent());
            if (examQuestionsInfos.get(i).getExamQuestionsType().equals("1")) {
            	row.createCell(2).setCellValue("判断题");
            	row.createCell(3).setCellValue("");
                row.createCell(4).setCellValue("");
                row.createCell(5).setCellValue("");
                row.createCell(6).setCellValue("");
             //   row.createCell(7).setCellValue("");
                row.createCell(7).setCellValue(examQuestionsInfos.get(i).getRightKey());
			}else if (examQuestionsInfos.get(i).getExamQuestionsType().equals("2")) {
				row.createCell(2).setCellValue("单项选择");
				row.createCell(3).setCellValue(examQuestionsInfos.get(i).getListExamQuestionsItem().get(0).getItemContent());
	            row.createCell(4).setCellValue(examQuestionsInfos.get(i).getListExamQuestionsItem().get(1).getItemContent());
	            row.createCell(5).setCellValue(examQuestionsInfos.get(i).getListExamQuestionsItem().get(2).getItemContent());
	            row.createCell(6).setCellValue(examQuestionsInfos.get(i).getListExamQuestionsItem().get(3).getItemContent());
	           // row.createCell(7).setCellValue("");
	            row.createCell(7).setCellValue(examQuestionsInfos.get(i).getRightKey());
		}else {
			row.createCell(2).setCellValue("多项选择");
			row.createCell(3).setCellValue(examQuestionsInfos.get(i).getListExamQuestionsItem().get(0).getItemContent());
            row.createCell(4).setCellValue(examQuestionsInfos.get(i).getListExamQuestionsItem().get(1).getItemContent());
            row.createCell(5).setCellValue(examQuestionsInfos.get(i).getListExamQuestionsItem().get(2).getItemContent());
            row.createCell(6).setCellValue(examQuestionsInfos.get(i).getListExamQuestionsItem().get(3).getItemContent());
            try {
            	examQuestionsInfos.get(i).getListExamQuestionsItem().get(4);
            	row.createCell(7).setCellValue(examQuestionsInfos.get(i).getListExamQuestionsItem().get(4).getItemContent());
			} catch (Exception e) {
				row.createCell(7).setCellValue("");
			}
            row.createCell(8).setCellValue(examQuestionsInfos.get(i).getRightKey());
            
        }
          //写入文件
    	  FileOutputStream out = new FileOutputStream(file.getPath()+"/"+examQuestionsName+".xlsx");
    	  wk.write(out);
          out.close();
         // wk.close();
		}
		
		return CommonResponseUtil.constructResponse(CommonResponseUtil.SUCCESS, "导出成功到"+file.getPath()+"/"+examQuestionsName+".xlsx");
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值