栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java解析Excel文件(.xlsx和.xls格式均适用)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java解析Excel文件(.xlsx和.xls格式均适用)

pom


    
      org.apache.poi
      poi
      3.15
    

    
    
      org.apache.poi
      poi-ooxml
      3.15
    

1,解析本地

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");

        //excel文件路径
        String excelPath = "E:\readExcelMaven\test.xlsx";

        try {
            //String encoding = "GBK";
            File excel = new File(excelPath);
            if (excel.isFile() && excel.exists()) {   //判断文件是否存在

                String[] split = excel.getName().split("\.");  //.是特殊字符,需要转义!!!!!
                Workbook wb;
                //根据文件后缀(xls/xlsx)进行判断
                if ( "xls".equals(split[1])){
                    FileInputStream fis = new FileInputStream(excel);   //文件流对象
                    wb = new HSSFWorkbook(fis);
                }else if ("xlsx".equals(split[1])){
                    wb = new XSSFWorkbook(excel);
                }else {
                    System.out.println("文件类型错误!");
                    return;
                }

                //开始解析
                Sheet sheet = wb.getSheetAt(0);     //读取sheet 0

                int firstRowIndex = sheet.getFirstRowNum()+1;   //第一行是列名,所以不读
                int lastRowIndex = sheet.getLastRowNum();
                System.out.println("firstRowIndex: "+firstRowIndex);
                System.out.println("lastRowIndex: "+lastRowIndex);

                for(int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex++) {   //遍历行
                    System.out.println("rIndex: " + rIndex);
                    Row row = sheet.getRow(rIndex);
                    if (row != null) {
                        int firstCellIndex = row.getFirstCellNum();
                        int lastCellIndex = row.getLastCellNum();
                        for (int cIndex = firstCellIndex; cIndex < lastCellIndex; cIndex++) {   //遍历列
                            Cell cell = row.getCell(cIndex);
                            if (cell != null) {
                                System.out.println(cell.toString());
                            }
                        }
                    }
                }
            } else {
                System.out.println("找不到指定的文件");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

2.上传解析

    @Override
    public void upload(MultipartFile file) throws IOException {
        Assert.isTrue(file.getSize() != 0, "文件内容为空,请重新选择文件!");
        String fileName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);

        Workbook wb;
        InputStream inputStream = file.getInputStream();
        //根据文件后缀(xls/xlsx)进行判断
        if ("xls".equals(fileName)) {
            wb = new HSSFWorkbook(inputStream);
        } else if ("xlsx".equals(fileName)) {
            wb = new XSSFWorkbook(inputStream);
        } else {
            System.out.println("文件类型错误!");
            return;
        }
        //开始解析
        //读取sheet 0
        Sheet sheet = wb.getSheetAt(0);
        //第一行标题,第二行是列名,所以不读
        int firstRowIndex = sheet.getFirstRowNum() + 2;
        int lastRowIndex = sheet.getLastRowNum();
        logger.info("firstRowIndex: {}", firstRowIndex);
        logger.info("lastRowIndex: {} ", lastRowIndex);
        List list = new ArrayList<>();
        //遍历行
        for (int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex++) {
            logger.info("rIndex: {} ", rIndex);
            Row row = sheet.getRow(rIndex);
            if (row != null) {
              
                  int firstCellIndex = row.getFirstCellNum();
                    int lastCellIndex = row.getLastCellNum();
                    //遍历列
                    for (int cIndex = firstCellIndex; cIndex < lastCellIndex; cIndex++) {
                        System.out.println(cIndex);
                        Cell cell = row.getCell(cIndex);

                        if (cell != null) {
                            System.out.println(cell.toString());
                        }
                    }
            }
        }
        this.saveBatch(list);
    }

下篇下载本地xslx

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/704687.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号