美文网首页
上传excel文件并读取内容

上传excel文件并读取内容

作者: i小雨 | 来源:发表于2020-08-14 10:09 被阅读0次

1.POI方法:

@RequestMapping(value = "author/uploadOOBInputExcel" , method = RequestMethod.POST)
    public void uploadOOBInputExcel(
            @RequestParam MultipartFile file,
            HttpServletRequest request,HttpServletResponse response) {
        
        Workbook wb = null;
        String filename = file.getOriginalFilename();
        
        List<Coo> cooList = new ArrayList<>();
        try {
            InputStream inputStream = file.getInputStream();
            //  根据后缀名是否excel文件
            if (filename.endsWith("xls")) {
                //  2003版本
                wb = new HSSFWorkbook(inputStream);
            } else if (filename.endsWith("xlsx")) {
                //  2007版本
                wb = new XSSFWorkbook(inputStream);
            }
            // 2.读取页脚sheet 
            Sheet sheetAt = wb.getSheetAt(0); 
            int firstRowNum = 1;
            int lastRowNum = sheetAt.getPhysicalNumberOfRows();
            // 3.循环读取某一行 
            for (int i = firstRowNum; i < lastRowNum; i++) {
                Coo coo = new Coo();
                String ppn = sheetAt.getRow(i).getCell(1).getStringCellValue();
                coo.setPpn(ppn);
                String co = sheetAt.getRow(i).getCell(8).getStringCellValue();
                coo.setCoo(co);
                cooList.add(coo);
            }
            int size = cooList.size();
            System.out.println(size);
        } catch(Exception e) { 
            e.printStackTrace(); 
        }
    }
  1. jxl方式(只能解析2003版本的-以.xls为后缀),但次方式解析简单直接,不需要考虑excel单元格的格式问题
    [参考于]https://www.cnblogs.com/bretgui/p/10156141.html

相关文章

网友评论

      本文标题:上传excel文件并读取内容

      本文链接:https://www.haomeiwen.com/subject/eptgdktx.html