java poi (1)读取表格数据

1.添加依赖

maven中写入

 <!-- 2007 xls 版本   -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.17</version>
        </dependency>


<!--        2013 xlsx版本-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.17</version>
        </dependency>

2.java代码

public static void main(String[] args) throws Exception {

//        FileInputStream fileInputStream = new FileInputStream("E:\\testfile\\台账.xls");
        FileInputStream fileInputStream = new FileInputStream(path);

//      讲输入流转为Workbook
        Workbook workbook = new XSSFWorkbook(fileInputStream);

//        得到总表数
        int sheet_num = workbook.getNumberOfSheets();
        System.out.println(sheet_num);

//        得到表1
        Sheet sheet = workbook.getSheetAt(0);

//        总行数
        int rows = sheet.getLastRowNum()+1;
        System.out.println(rows);


        int row_first = sheet.getFirstRowNum();
        int row_last = sheet.getLastRowNum();


//        得到第一行
        Row row = sheet.getRow(0);



//        得到单元格
        Cell cell = row.getCell(1);
        System.out.println(row.getLastCellNum());
        System.out.println(row.getFirstCellNum());




        System.out.println(cell.getStringCellValue());

        fileInputStream.close();


    }

3.注意 

Workbook workbook = new XSSFWorkbook(fileInputStream);  //xlsx
Workbook workbook = new HSSFWorkbook(fileInputStream);  //xls

版权声明:本文为yellow_flowers原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。