excel导入,springboot示例

excel导入,springboot示例

 

 

@PostMapping("/importExcel")
public AjaxResult importExcel(MultipartFile file) {
    System.out.println("file" + file.getSize());
    int addNum=0;//导入的行数 计数
    try {
        // MultipartFile file 是用来接收前端传递过来的文件
        // 创建workbook对象,读取整个文档
        InputStream inputStream = file.getInputStream();
        //POIFSFileSystem poifsFileSystem = new POIFSFileSystem(inputStream);
        XSSFWorkbook wb = new XSSFWorkbook(inputStream);
        // 读取页脚sheet
        XSSFSheet sheetAt = wb.getSheetAt(0);

        // 循环读取某一行
        int index = 0;

        for (Row row : sheetAt) {
            // 读取每一行的单元格
            if (index == 0) {
                index++;
                continue;
            }
            //创建一个对象
            AppSubject subject = new AppSubject();

            //题目名称 row.getCell(0).getStringCellValue() 为excel 下标0位置的值
            subject.setSubjectName("<p><strong>"+row.getCell(0).getStringCellValue()+"</p></strong>");

            //excel列为number类型 需要转成字符串  row.getCell(1).setCellType(CellType.STRING);
            //正确答案
            row.getCell(1).setCellType(CellType.STRING);
            subject.setRightOption(row.getCell(1).getStringCellValue());

           //执行自己保存的逻辑
           int result=  appSubjectService.importSubject(subject);
          //行数+1
            addNum +=result;

        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
//返回导入的行数
    return AjaxResult.success(addNum);
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值