• 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();  
            }  
  • 相关阅读:
    Java 对象的序列化和反序列化
    Java 数组元素倒序的三种方式
    Java实现字符串倒序输出的几种方法
    sql ,内连接,外连接,自然连接等各种连接
    linux上 安装软件
    打乱数组
    java集合类
    我换了新博客啦
    代理模式
    抽象工厂模式
  • 原文地址:https://www.cnblogs.com/dhName/p/14560384.html
Copyright © 2020-2023  润新知