使用POI实现Excel文件的读取(超详细)

目录

一 导入poi相关的maven坐标

二 实现创建并且写入文件

2.1实现步骤

 2.2实现代码

2.3效果展示

​编辑 

2.4注意

三 实现从Excel文件中读取数据 

3.1实现步骤

3.2实现代码

 3.3结果展示


一 导入poi相关的maven坐标

        <!-- Apache poi -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
        </dependency>

二 实现创建并且写入文件

2.1实现步骤

        往Excel文件中写入数据大致分为五步:

        1.创建excel文件

        2.创建excel文件的sheet页

        3.在sheet页中创建表格的行和列,即单元格

        4.在对应单元格中写入数据

        5.指定创建文件的路径

        6.关闭资源

 2.2实现代码

创建Excel文件并且写入数据的代码:

 /**
     * 通过POI创建excel文件并且向文件中写入数据
     */
    public static void write() throws Exception {
        // 1.创建一个新的excel文件
        XSSFWorkbook excel = new XSSFWorkbook(); // 通过这个方法是在内存中创建的
        // 2.在excel文件中创建一个sheet标签页
        XSSFSheet sheet = excel.createSheet("info"); // info是这个sheet的名字,不是excel文件的名字

        // 3.在sheet标签页中创建行对象
        XSSFRow row = sheet.createRow(0); // 下标从0开始,0代表第一行

        // 4.在行上面创建单元格并写入数据
        row.createCell(0).setCellValue("序号"); // 第一列
        row.createCell(1).setCellValue("姓名"); // 第二列
        row.createCell(2).setCellValue("年龄"); // 第三列

        // 然后在第二行对应的列下面写入相应的值
        XSSFRow row2 = sheet.createRow(1);
        row2.createCell(0).setCellValue("1"); // 序号为1
        row2.createCell(1).setCellValue("张三"); // 姓名为张三
        row2.createCell(2).setCellValue("99"); // 年龄为18

        // 在第三行对应的列下面写入对应的值
        XSSFRow row3 = sheet.createRow(2);
        row3.createCell(0).setCellValue("2");
        row3.createCell(1).setCellValue("李四");
        row3.createCell(2).setCellValue("23");

        // ... 后面按照需要自己继续创建单元格写内容

        // 5.最后指定excel文件写入的地址
        FileOutputStream fileOutputStream = new FileOutputStream(new File("D:\\feisi\\cangQiong\\info.xlsx"));
        excel.write(fileOutputStream);

        // 6.关闭资源
        fileOutputStream.close();
        excel.close();
    }

2.3效果展示

 

文件内容:

2.4注意

        这里有一个点特别需要注意,就是这段往单元格中写入数据的代码:

        // 然后在第二行对应的列下面写入相应的值
        XSSFRow row2 = sheet.createRow(1);
        row2.createCell(0).setCellValue("1"); // 序号为1
        row2.createCell(1).setCellValue("张三"); // 姓名为张三
        row2.createCell(2).setCellValue("99"); // 年龄为18

在上面这段代码中 , 我们经常会因为方便,不先给对象引入变量row2 , 而是直接调用创建列的方法,然后写入数据,这样会导致数据写不进去 , 如下:

        // 然后在第二行对应的列下面写入相应的值
        // XSSFRow row2 = sheet.createRow(1);
        sheet.createRow(1).createCell(0).setCellValue("1"); // 序号为1
        sheet.createRow(1).createCell(1).setCellValue("张三"); // 姓名为张三
        sheet.createRow(1).createCell(2).setCellValue("99"); // 年龄为18

 那么得到的表格就会数据缺失,结果如下:

三 实现从Excel文件中读取数据 

3.1实现步骤

读取excel文件大致分为五步:

1.获取指定的excel文件对象

2.获取指定该文件对象的sheet页

3.遍历该sheet页中的行

4.遍历每一行的所有列并且获取该单元格的数据

5.关闭资源

3.2实现代码

读取excel文件方法实现代码如下:

  public static void read() throws Exception{
//        1.还是一样得到一个excel文件对象,但是我们这里是读 , 所以要指定文件路径
        FileInputStream fileInputStream = new FileInputStream(new File("D:\\feisi\\cangqiong\\info.xlsx"));
        XSSFWorkbook excel = new XSSFWorkbook(fileInputStream);
//        2.读取excel文件中的sheet1页(因为这个文件就只创建了一个sheet页)
//        XSSFSheet sheet = excel.getSheetAt(0);//这是直接拿第一个sheet页
        XSSFSheet sheet = excel.getSheet("info");//这是根据sheet页的名字来拿sheet页
//        3.遍历所有行,但是我不知道有多少行,所以我可以直接获取最后有文字的那一行,这样就知道一个有多少行了
        int lastRowNum = sheet.getLastRowNum();//获取有文字的最后一行行号
        for (int i = 0 ; i<=lastRowNum ; ++i){
//            获得某一行
            XSSFRow row = sheet.getRow(i);
//            4.遍历该行得所有单元格对象,并且获取该单元格对象中得数据
            for (Cell cell : row){
                String stringCellValue = cell.getStringCellValue();
                System.out.print(stringCellValue+"\t");
            }
//            读完一行的数据后换行
            System.out.println();
        }
//        5.关闭资源
        excel.close();
        fileInputStream.close();
    }

 3.3结果展示

调用该方法,得到输出的excel中获取到的数据:

本次分享就在这里 , 谢谢大家,加油! 

相关推荐

  1. 使用POI技术实现excel文件导入

    2024-07-09 17:30:05       53 阅读
  2. 使用 Apache POI XDGF 读取 vsdx 文件

    2024-07-09 17:30:05       47 阅读
  3. js实现读取excel文件

    2024-07-09 17:30:05       37 阅读
  4. 使用 R 读取Excel文件特定列

    2024-07-09 17:30:05       33 阅读
  5. 使用 openpyxl 库读取 Excel 文件

    2024-07-09 17:30:05       48 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-09 17:30:05       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-09 17:30:05       71 阅读
  3. 在Django里面运行非项目文件

    2024-07-09 17:30:05       58 阅读
  4. Python语言-面向对象

    2024-07-09 17:30:05       69 阅读

热门阅读

  1. android Gradle储蓄地址

    2024-07-09 17:30:05       22 阅读
  2. 基于BERT的大规模文本处理实战

    2024-07-09 17:30:05       25 阅读
  3. 【LeetCode 0242】【Map/排序】有效的异位词

    2024-07-09 17:30:05       20 阅读
  4. Ubuntu下Qt-5.12.9创建快捷方式到桌面

    2024-07-09 17:30:05       26 阅读
  5. ArkTs基础入门

    2024-07-09 17:30:05       24 阅读
  6. 代码随想录Day74(图论Part10)

    2024-07-09 17:30:05       29 阅读
  7. 如何使一个盒子水平垂直居中(常用的)

    2024-07-09 17:30:05       22 阅读
  8. mybatis用注解替换xml,不再写.xml了

    2024-07-09 17:30:05       28 阅读
  9. 拓扑学习系列(9)计算代数拓扑中的复形COMPLEXES

    2024-07-09 17:30:05       29 阅读
  10. Docker

    Docker

    2024-07-09 17:30:05      29 阅读
  11. 服务器安装多个Tomcat

    2024-07-09 17:30:05       26 阅读