服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Java使用apache poi操作excel的方式

Java使用apache poi操作excel的方式

2023-02-07 14:07寻觅beyond Java教程

这篇文章主要介绍了Java使用apache poi进行excel相关操作,本文主要针对Apache POI对excel的操作进行介绍,主要包括如何创建一个excel、录入数据、读取excel数据的方式,需要的朋友可以参考下

一.基本介绍

1.1、Apache POI介绍

  Apache POI是一个可以进行微软的文档进行开源库,可以操作的文档类型包括word、ppt、excel、visio、outlook....

  本文主要针对Apache POI对excel的操作进行介绍,主要包括如何创建一个excel、录入数据、读取excel数据的方式。

  参考官方文档:

  1.首页:https://poi.apache.org/

  2.Excel文档介绍,使用方式

1.2、HSSF和XSSF

  在POI中,如果要操作Excel,可以有两种操作“模式”,分别是HSSF和XSSF,其中HSSF可以处理win97以及低于2007的excel,而XSSF可以用于处理2007以及更高的版本excel,所以我们一般都会使用XSSF。

  有一个很重要的术语:工作薄,其实可以理解为“文档”,excel、word,都属于工作簿,英文workbook。

1.3、引入依赖

  只需要导入poi-ooxml这个库即可。

<dependencies>
  <!-- poi库 -->
  <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml</artifactId>
      <version>4.1.2</version>
  </dependency>

  <!-- 示例以单元测试的形式,所以需要导入junit -->
  <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13</version>
  </dependency>
</dependencies>

 

二.利用poi库创建excel

2.1、创建一个空excel

  下面的代码运行后

package cn.ganlixin.poi;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;
import java.io.*;
public class CreateExcelTest {
  /**
   * 创建一个空的Excel文件
   */
  @Test
  public void testCreateEmptyExcel() throws IOException {
      // 创建文件
      BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream("data.xlsx"));
      // 创建工作簿类(所有要写入excel的数据,都将保存在workbook中)
      XSSFWorkbook workbook = new XSSFWorkbook();
      // 将workbook中的数据写入到文件中。
      workbook.write(outputStream);
      // 关闭
      workbook.close();
      outputStream.close();
  }
}

2.2、简单演示写入excel内容

  下面演示创建一个data.xlsx,并在“my-sheet-1”sheet内的第5行第3列写入一个“hello world”:

package cn.ganlixin.poi;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;
import java.io.*;
public class CreateExcelTest {
  /**
   * 简单演示如何写入数据内容
   */
  @Test
  public void testCreateSimpleWorkbook() throws IOException {
      // 指定创建的excel文件名称
      BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream("data.xlsx"));
      // 定义一个工作薄(所有要写入excel的数据,都将保存在workbook中)
      XSSFWorkbook workbook = new XSSFWorkbook();
      // 创建一个sheet
      XSSFSheet sheet = workbook.createSheet("my-sheet-1");
      // 开始写入数据流程,2大步:1、定位到单元格,2、写入数据;定位单元格,需要通过行、列配合指定。
      // step1: 先选择第几行(0表示第一行),下面表示在第6行
      XSSFRow row = sheet.createRow(5);
      // step2:选择第几列(0表示第一列),注意,这里的第几列,是在上面指定的row基础上,也就是第6行,第3列
      XSSFCell cell = row.createCell(2);
      // step3:设置单元格的数据(写入数据)
      cell.setCellValue("hello world");
      // 执行写入操作
      workbook.write(outputStream);
      workbook.close();
      outputStream.flush();
      outputStream.close();
  }
}

  运行后,下面是生成的excel:

Java使用apache poi操作excel的方式

2.3、通常的写入数据流程

  一般写入excel的数据都是结构化的(类似于数据库表的结构),下面是一个示例,创建一个users.xlsx文档:

package cn.ganlixin.poi;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class CreateExcelTest {
  private static class User {
      private Integer id;
      private String name;
      private Integer age;
      private String addr;
      // 避免占用篇幅,所以省略了构造方法、getter、setter
  }
  /**
   * 写入结构化的用户信息数据
   */
  @Test
  public void testCreateUsersExcel() throws IOException {
      List<User> userList = new ArrayList<>();
      userList.add(new User(1, "abc", 99, "北京"));
      userList.add(new User(2, "lol", 77, "上海"));
      userList.add(new User(3, "qaq", 88, "深圳"));
      userList.add(new User(4, "owo", 66, "杭州"));
      // 指定创建的excel文件名称
      BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream("users.xlsx"));
      // 定义一个工作薄(所有要写入excel的数据,都将保存在workbook中)
      XSSFWorkbook workbook = new XSSFWorkbook();
      // 创建一个sheet
      XSSFSheet sheet = workbook.createSheet("my-sheet");
      // 第一行,为表头,需要单独写入,下面是错误方式,因为在反复的创建第1行(rowNumber为0)
      /*
      sheet.createRow(0).createCell(0).setCellValue("编号");
      sheet.createRow(0).createCell(1).setCellValue("姓名");
      sheet.createRow(0).createCell(2).setCellValue("年龄");
      sheet.createRow(0).createCell(3).setCellValue("城市");
       */
      // 正确方式
      XSSFRow head = sheet.createRow(0);
      head.createCell(0).setCellValue("编号");
      head.createCell(1).setCellValue("姓名");
      head.createCell(2).setCellValue("年龄");
      head.createCell(3).setCellValue("城市");
      // 接下来遍历要录入的数据(建议使用for,并且从第2行开始,也就是rowNumber为1,因为表头占了一行)
      for (int rowNumber = 1, index = 0; index < userList.size(); index++, rowNumber++) {
          User user = userList.get(index);
          // 写入数据流程,1、定位到单元格,2、写入数据;定位单元格,需要通过行、列配合指定。
          XSSFRow row = sheet.createRow(rowNumber);
          row.createCell(0).setCellValue(user.getId());
          row.createCell(1).setCellValue(user.getName());
          row.createCell(2).setCellValue(user.getAge());
          row.createCell(3).setCellValue(user.getAddr());
      }
      // 执行写入操作
      workbook.write(outputStream);
      workbook.close();
      outputStream.flush();
      outputStream.close();
  }
}

  生成的excel如下:

Java使用apache poi操作excel的方式

 

三.使用POI读取Excel内容

3.1、读取excel示例

  下面针对上面创建的users.xlsx进行读取并打印

package cn.ganlixin.poi;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class ReadExcelTest {
  @Test
  public void testReadUsersExcel() throws IOException {
      // 指定excel文件,创建缓存输入流
      BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream("users.xlsx"));
      // 直接传入输入流即可,此时excel就已经解析了
      XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
      // 选择要处理的sheet名称
      XSSFSheet sheet = workbook.getSheet("my-sheet");
      // 第一行表头,单独处理
      // 迭代遍历sheet剩余的每一行
      for (int rowNum = 0; rowNum < sheet.getPhysicalNumberOfRows(); rowNum++) {
          if (rowNum == 0) { // 读取第一行(表头)
              XSSFRow head = sheet.getRow(rowNum);
              String headColumn_1 = head.getCell(0).getStringCellValue();
              String headColumn_2 = head.getCell(1).getStringCellValue();
              String headColumn_3 = head.getCell(2).getStringCellValue();
              String headColumn_4 = head.getCell(3).getStringCellValue();
              String headStr = String.format("%s\t%s\t%s\t%s", headColumn_1, headColumn_2, headColumn_3, headColumn_4);
              System.out.println(headStr);
          } else { // 非表头(注意读取的时候要注意单元格内数据的格式,要使用正确的读取方法)
              XSSFRow row = sheet.getRow(rowNum);
              int id = (int) row.getCell(0).getNumericCellValue();
              String name = row.getCell(1).getStringCellValue();
              int age = (int) row.getCell(2).getNumericCellValue();
              String addr = row.getCell(3).getStringCellValue();
              String rowContent = String.format("%s\t%s\t%s\t%s", id, name, age, addr);
              System.out.println(rowContent);
          }
      }
      workbook.close();
      inputStream.close();
  }
}

  运行输出如下:

Java使用apache poi操作excel的方式

到此这篇关于Java使用apache poi进行excel相关操作的文章就介绍到这了,更多相关java  apache poi操作excel内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/-beyond/p/12764501.html

延伸 · 阅读

精彩推荐
  • Java教程java多线程Future和Callable类示例分享

    java多线程Future和Callable类示例分享

    JAVA多线程实现方式主要有三种:继承Thread类、实现Runnable接口、使用ExecutorService、Callable、Future实现有返回结果的多线程。其中前两种方式线程执行完后都...

    hebedich2622020-03-21
  • Java教程Java实现获取小程序带参二维码并保存到本地

    Java实现获取小程序带参二维码并保存到本地

    这篇文章主要介绍了Java实现获取小程序带参二维码并保存到本地,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...

    Moshow郑锴7392022-02-20
  • Java教程java中多态概念、实现原理详解

    java中多态概念、实现原理详解

    JAVA中多态性是对象多种表现形式的体现。在面向对象中,最常见的多态发生在使用父类的引用来引用子类的对象。下面这篇文章主要给大家介绍一下,需要...

    4719126195462020-09-10
  • Java教程从零开始学JAVA之可变参数

    从零开始学JAVA之可变参数

    本文是从零开始学JAVA的第一篇,属于Java基础知识介绍的第一部分,主要介绍Java的可变参数,非常使用,希望对大家有所帮助 ...

    hebedich2422019-12-03
  • Java教程Java Buffer缓冲区(NIO)

    Java Buffer缓冲区(NIO)

    Java NIO(New IO)是从Java 1.4版本开始引入的一个新的IO API,可以替代标准的Java IO API。本系列教程将有助于你学习和理解Java NIO。...

    阿昌喜欢吃黄桃8922021-12-28
  • Java教程Java Web项目中使用Socket通信多线程、长连接的方法

    Java Web项目中使用Socket通信多线程、长连接的方法

    很多时候在javaweb项目中我们需要用到Socket通信来实现功能,在web中使用Socket我们需要建立一个监听程序,在程序启动时,启动socket监听。接下来通过本文给...

    程序员3532020-04-21
  • Java教程java FileWriter 追加文件及文件改名方式

    java FileWriter 追加文件及文件改名方式

    这篇文章主要介绍了java FileWriter 追加文件及文件改名的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...

    秦学强8892021-10-18
  • Java教程java集合_浅谈Iterable和Iterator的区别

    java集合_浅谈Iterable和Iterator的区别

    下面小编就为大家带来一篇java集合_浅谈Iterable和Iterator的区别。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧 ...

    jingxian4912020-06-12