springboot上传Excel并解析

@PostMapping(value = "importLibExcel")
public String  uploadLibExcel(HttpServletRequest request, @RequestParam("upload")MultipartFile file, Model model) throws IOException {
    String msg = "";
    int count = 1;
    byte[] bytes = file.getBytes();
    Long currentTimeMillis = System.currentTimeMillis();
    Path path = Paths.get("D:\\Imp_upload\\" + currentTimeMillis+"_"+file.getOriginalFilename());
    Files.write(path,bytes);

    File f = new File(path.toString());
    FileInputStream fileInputStream = new FileInputStream(f);

    Workbook workbook = new HSSFWorkbook(fileInputStream);
    fileInputStream.close();
    Sheet sheet = workbook.getSheetAt(0);
    byte[] b = new byte[1024];
    Iterator<Row> iterator = sheet.rowIterator();
    if (iterator.hasNext()){
        iterator.next();
    }

    while (iterator.hasNext()){
        try {
            Row row = iterator.next();
            Cell libType = row.getCell(0);
            Cell libSub  = row.getCell(1);
            Cell answerA = row.getCell(2);
            Cell answerB = row.getCell(3);
            Cell answerC = row.getCell(4);
            Cell answerD = row.getCell(5);
            Cell result  = row.getCell(6);

            logger.info("libType:"+libType);
            logger.info("libSub:"+libSub);
            logger.info("answerA:"+answerA);
            logger.info("answerB:"+answerB);
            logger.info("answerC:"+answerC);
            logger.info("answerD:"+answerD);
            logger.info("result:"+result);
            count ++;
        }catch (Exception e){
            e.printStackTrace();
            msg = "在导入第"+count+"题时报错;已导入"+count+"道题";
            break;
        }
    }
    if ("".equals(msg)){
        msg = "导入成功!共导入"+count+"条试题";
    }
    model.addAttribute("msg",msg);
    return "system/import_result";
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值