Apache POI

一、介绍

Apache POl是一个处理Miscrosoft Office各种文件格式的开源项目。简单来说就是,我们可以使用POI在Java程序中对Miscrosoft Office各种文件进行读写操作。

一般情况下,POI都是用于操作 Excel文件。

准备工作:

在pom文件中导入依赖---

复制代码
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
        </dependency>

二、操作方法

1、通过POI向Excel文件写入内容
复制代码
/**
     * 通过POI创建Excel文件并写入文件内容
     */
    private static void write() throws Exception {
//        在内存中创建一个Excel文件
        XSSFWorkbook excel = new XSSFWorkbook();
//        在Excel文件中创建一个Sheet页
        XSSFSheet sheet = excel.createSheet("info");
//        在Sheet页中创建行对象,rownum编号从0开始
        XSSFRow row = sheet.createRow(1);
//        创建单元格并且写入文件内容
        row.createCell(1).setCellValue("姓名");
        row.createCell(2).setCellValue("城市");

//        创建一个新行
        row = sheet.createRow(2);
        row.createCell(1).setCellValue("张三");
        row.createCell(2).setCellValue("北京");

        row = sheet.createRow(3);
        row.createCell(1).setCellValue("李四");
        row.createCell(2).setCellValue("南京");

//        通过输出流将内存中的Excel文件写入到磁盘
        FileOutputStream out = new FileOutputStream(new File("F:\\info.xlsx"));
        excel.write(out);

//        关闭资源
        out.close();
        excel.close();
    }
2、 通过POI将Excel文件内容读出

其中 i的变量为1,也就是说我是从表第二行开始读的,如果你第一行有数据,就将i的变量改为0.

复制代码
    /**
     * 通过POI读取Excel文件中的内容
     *
     * @throws Exception
     */
    private static void read() throws Exception {
        FileInputStream in = new FileInputStream(new File("F:\\info.xlsx"));

//      读取磁盘上已经存在的Excel文件
        XSSFWorkbook excel = new XSSFWorkbook(in);
//        读取Excel文件中的Sheet页
        XSSFSheet sheet = excel.getSheetAt(0);

//        获取sheet中最后一行的行号
        int lastRowNum = sheet.getLastRowNum();

        for (int i = 1; i <= lastRowNum; i++) {
//            获得某一行
            XSSFRow row = sheet.getRow(i);
//            获得单元格对象
            String cellValue1 = row.getCell(1).getStringCellValue();
            String cellValue2 = row.getCell(2).getStringCellValue();
            System.out.println(cellValue1 + " " + cellValue2);
        }

//        关闭资源
        in.close();
        excel.close();
    }
相关推荐
不爱编程的小九九4 小时前
小九源码-springboot097-java付费自习室管理系统
java·开发语言·spring boot
独自破碎E4 小时前
LeetCode 381: O(1) 时间插入、删除和获取随机元素 - 允许重复
java·算法·leetcode
程语有云4 小时前
生产事故-Caffeine缓存误用之临下班的救赎
java·缓存·caffeine·阻塞·log·生产事故
Miraitowa_cheems4 小时前
LeetCode算法日记 - Day 81: 最大子数组和
java·数据结构·算法·leetcode·决策树·职场和发展·深度优先
CodeCraft Studio4 小时前
国产化Word处理控件Spire.Doc教程:用Java实现TXT文本与Word互转的完整教程
java·c#·word·spire.doc·word文档转换·txt转word·word转txt
徐子童5 小时前
数据结构---优先级队列(堆)
java·数据结构·面试题·优先级队列··topk问题
滑水滑成滑头5 小时前
**标题:发散创新:智能交通系统的深度探究与实现**摘要:本文将详细
java·人工智能·python
siriuuus5 小时前
Maven 核心概念及生命周期
java·maven
SelectDB5 小时前
Apache Doris 4.0 版本正式发布:全面升级 AI 与搜索能力,强化离线计算
数据库·数据分析·apache
SelectDB5 小时前
从 Snowflake 到 Apache Doris:Planet 实时分析成本直降 80%、查询加速 90 倍
数据库·数据分析·apache