java操作Excel、Word等

Java读取Excel

1. 引入pom依赖

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

2. POI结构

HSSF - 提供读写Microsoft Excel XLS格式档案的功能
XSSF - 提供读写Microsoft Excel OOXML XLSX格式档案的功能
HWPF - 提供读写Microsoft Word DOC格式档案的功能
HSLF - 提供读写Microsoft PowerPoint格式档案的功能
HDGF - 提供读Microsoft Visio格式档案的功能
HPBF - 提供读Microsoft Publisher格式档案的功能
HSMF - 提供读Microsoft Outlook格式档案的功能

3. 读取实例

public void test1() throws IOException {
        //加载指定文件,创建一个Excel对象(工作溥
        XSSFWorkbook excel = new XSSFWorkbook(new FileInputStream(new File("C:\\Users\\lenovo\\Desktop\\poi.xlsx")));
        //读取Excel文件中第一个Sheet标签页
        XSSFSheet sheetAt = excel.getSheetAt(0);
        //遍历Sheet标签页,获得每一行数据
        for (Row cells : sheetAt) {
            //遍历行,后的每个单元格
            for (Cell cell : cells) {
                System.out.println(cell.getStringCellValue());
            }
        }
        //关闭资源
        excel.close();
    }
posted @ 2022-06-20 00:05  又一岁荣枯  阅读(88)  评论(0编辑  收藏  举报