java操作excel-----poi

一、所需依赖包

  1、使用maven会自动导入相关依赖,所以只需要导入2007版的的包,其他包自动导入,包括2003所需jar包。

    

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.17</version>
        </dependency>

  2、如果需要直接导包,下面这些包全部下载下来,手动导入。

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</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.xmlbeans</groupId>
            <artifactId>xmlbeans</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>3.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>4.3</version>
        </dependency>        

    

二、excel读取

  1、数据模板

    

  2、演示代码

    

    
    @SuppressWarnings("resource")
    public static void main(String[] args) {
        //excel所在地址
        String path = "E:\\demo\\excel\\one.xlsx";
        
        File file = new File(path);
        InputStream in = null;
        
        try {
            in = new FileInputStream(file);
            //文件后缀名
            String type = path.substring(path.indexOf(".")+1);
            Workbook wb;
            
            //根据后缀名选择不同版本的实现2003/2007版,分别对应xls/xlsx
            if("xlsx".equals(type) || "XlSX".equals(type)) {
                wb = new XSSFWorkbook(in);
            }else if("xls".equals(type) || "XLS".equals(type)) {
                wb = new HSSFWorkbook(in);
            }else {
                System.out.println("文件后缀格式有误");
                return;
            }
            //获取sheet
            Sheet sheet = wb.getSheetAt(0);
            //总行数
            int rsRows = sheet.getPhysicalNumberOfRows();
            //总列数
            int rsColumn = sheet.getRow(0).getPhysicalNumberOfCells();
            
            //存放excel的二维数组
            String[][] data = new String[rsRows][rsColumn];
//            Row row ;
            Cell cell;
            for(int i =0;i<rsRows;i++) {
                for(int j =0;j<rsColumn;j++) {
                    cell = sheet.getRow(i).getCell(j);
                    //把非string类型的单元格设置成string
                    cell.setCellType(CellType.STRING);
                    data[i][j] = cell.getStringCellValue();
                }
            }
            
            //打印结果
            for(int i=0;i<data.length;i++) {
                for(int j=0;j<data[i].length;j++) {
                    System.out.print(data[i][j]+"\t");
                }
                System.out.println();
            }
            
        } catch (FileNotFoundException e) {
            System.out.println("读取文件失败");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("创建workbook失败");
            e.printStackTrace();
        }finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
        
        
    
    }

结果: 

id    name    address    
1    吴    上海    
2    li    地球    
3    ki    火星

猜你喜欢

转载自www.cnblogs.com/whalesea/p/10973505.html