快速掌握并使用Apache POI

Apache POI

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


简单使用

快速入门

第一步:导入Apache POI的maven坐标:

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

如何使用POI操作Excel文件

我们先在一个目录下创建一个测试的test.xlsx文件
通过POI将数据写入到文件中

java 复制代码
@SpringBootTest
//使用POI操作Excel文件
public class POITest {
    /**
     * 通过POI创建excel文件并写入内容
     */
    public static void write() throws Exception{
        //在内存中创建一个Excel文件
        XSSFWorkbook excel = new XSSFWorkbook();
        //在Excel文件中创建一个Sheet页
        XSSFSheet sheet = excel.createSheet("info");
        //在Sheet页中创建行,参数行号下表是从0开始的
        XSSFRow row = sheet.createRow(1);
        //在指定行上创建单元格
        XSSFCell cell = row.createCell(1);
        //向单元格中写入内容
        cell.setCellValue("帅");
        //向第三个单元格写入数据
        row.createCell(2).setCellValue("真的帅");
        //通过输出流将内容写入到文件中,创建一个输出流
        FileOutputStream fileOutputStream = new FileOutputStream(new File("F:\\test.xlsx"));
        //通过Excel文件对象将输出流写入到文件中
        excel.write(fileOutputStream);
        //关闭资源
        fileOutputStream.close();
        excel.close(); 
    }
    public static void main(String[] args) throws Exception{
        write();
    }
}

这里创建Excel文件对象是在内存中创建的文件,最后结合输出流对象将文件写入到Excel文件当中

根据POI从文件中读取到内容

java 复制代码
    public static void read() throws Exception{
        //创建输入流,读取磁盘上已经存在的Excel文件
        FileInputStream fileInputStream = new FileInputStream(new File("F:\\test.xlsx"));
        XSSFWorkbook excel = new XSSFWorkbook(fileInputStream);
        //读取Excel文件中的第一个sheet页
        XSSFSheet sheet = excel.getSheet("info");
        //读取sheet页中的内容,首先应该得到有文字的最后一行的行号
        int lastRowNum = sheet.getLastRowNum();
        for (int i=1;i<=lastRowNum;i++)
        {
            XSSFRow row = sheet.getRow(i);
            String stringCellValue = row.getCell(1).getStringCellValue();
            String stringCellValue1 = row.getCell(2).getStringCellValue();
            System.out.println(stringCellValue+":"+stringCellValue1);
        }
        //关闭资源
        excel.close();
        fileInputStream.close();
    }

从文件中读就不能在内存中操作,需要将磁盘上的文件通过输入流的方式加载到Excel对象当中

相关推荐
探索宇宙真理.2 小时前
Apache Tomcat RewriteValve目录遍历漏洞 | CVE-2025-55752 复现
java·经验分享·tomcat·apache·安全漏洞
lang201509286 小时前
Apache Maven 项目的开发指南
java·maven·apache
SelectDB技术团队15 小时前
货拉拉用户画像基于 Apache Doris 的数据模型设计与实践
数据分析·汽车·apache·用户画像·货拉拉
码界奇点1 天前
Apache IoTDB 架构特性与 PrometheusGrafana 监控体系部署实践
架构·apache·grafana·prometheus·iotdb
一个天蝎座 白勺 程序猿2 天前
Apache IoTDB(7):设备模板管理——工业物联网元数据标准化的破局之道
数据库·apache·时序数据库·iotdb
惜.己2 天前
apache启动失败Failed to start The Apache HTTP Server.
apache
一个天蝎座 白勺 程序猿2 天前
Apache IoTDB(8):时间序列管理——从创建到分析的实战指南
数据库·apache·时序数据库·iotdb
SelectDB2 天前
更高效的数据处理解决方案:基于 MinIO 部署 Apache Doris 存算分离版本实践
数据库·数据分析·apache
Wang's Blog2 天前
Linux小课堂: Apache虚拟主机配置之基于IP与域名的服务器部署指南
linux·服务器·apache
Wang's Blog2 天前
Linux小课堂: Apache服务在CentOS上的安装与基础配置指南
linux·centos·apache