java excel maven

pom.xml

<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>3.17</version>
        </dependency>
         <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>3.17</version>
        </dependency>
          <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.17</version>
              </dependency>  
          <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-examples</artifactId>
            <version>3.17</version>
              </dependency>  
          <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-excelant</artifactId>
            <version>3.17</version>
              </dependency>  
          <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.17</version>
              </dependency>  
          <!-- https://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans -->
        <dependency>
            <groupId>org.apache.xmlbeans</groupId>
            <artifactId>xmlbeans</artifactId>
            <version>2.6.0</version>
        </dependency>

 

demo:

SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");  
        try {  
            //同时支持Excel 2003、2007  
            //Excel文件所在位置
            File excelFile = new File("D://aa.xlsx"); //创建文件对象  
            FileInputStream is = new FileInputStream(excelFile); //文件流  
            Workbook workbook = WorkbookFactory.create(is); //这种方式 Excel 2003/2007/2010 都是可以处理的  
            int sheetCount = workbook.getNumberOfSheets();  //Sheet的数量  
            //遍历每个Sheet  
            for (int s = 0; s < sheetCount; s++) {  
                Sheet sheet = workbook.getSheetAt(s);  
                int rowCount = sheet.getPhysicalNumberOfRows(); //获取总行数  
                //遍历每一行  
                for (int r = 0; r < rowCount; r++) {  
                    Row row = sheet.getRow(r);  
                    int cellCount = row.getPhysicalNumberOfCells(); //获取总列数  
                    //遍历每一个单元格  
                    
                 
                    for (int c = 0; c < cellCount; c++) {  
                        Cell cell = row.getCell(c);  
                        int cellType = cell.getCellType();  
                        String cellValue = null;
                        //在读取单元格内容前,设置所有单元格中内容都是字符串类型
                        cell.setCellType(Cell.CELL_TYPE_STRING);
                        //按照字符串类型读取单元格内数据
                        cellValue = cell.getStringCellValue();
                        /*在这里可以对每个单元格中的值进行二次操作转化*/
                        System.out.print(cellValue + "  "); 
                    }  
                    System.out.println(); 
                }  
            }  
      
        }  
        catch (Exception e) {  
            e.printStackTrace();  
        }  

 

posted @ 2021-03-20 12:15  _Meditation  阅读(90)  评论(0编辑  收藏  举报