Apache—POI详解、小案例展示

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

目录

1、应用场景

2、案例代码

[2.1 创建 Excel 文件](#2.1 创建 Excel 文件)

[2.2 读取 Excel 文件](#2.2 读取 Excel 文件)


1、应用场景

图 1-1 Apache POI应用场景
Apache POI应用场景: * 银行网银系统导出交易明细 * 各种业务系统导出Excel报表 * 批量导入业务数据

2、案例代码

2.1 创建 Excel 文件

java 复制代码
package com.sky.test;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class POITest {
    public static void main(String[] args) throws IOException {
        write();
    }

    /**
     * 通过POI创建Excel文件并且写入文件内容
     */
    public static void write() throws IOException {
        //创建一个Excel文件
        XSSFWorkbook excel = new XSSFWorkbook();
        //创建Excel中的单元测
        XSSFSheet sheet = excel.createSheet("info");
        //在sheet中创建行对象,rownum编号从零开始
        XSSFRow row = sheet.createRow(1);
        //创建单元格并且写入文件内容
        row.createCell(1).setCellValue("姓名");
        row.createCell(2).setCellValue("城市");
        //创建一个新行
        XSSFRow row1 = sheet.createRow(2);
        row1.createCell(1).setCellValue("王宁");
        row1.createCell(2).setCellValue("亳州");
        //创建一个新行
        XSSFRow row2 = sheet.createRow(2);
        row2.createCell(1).setCellValue("王宁");
        row2.createCell(2).setCellValue("亳州");
        //将内存中的Excel表格存储到指定的盘符下面
        FileOutputStream fileOutputStream = new FileOutputStream(new File("D:\\info.xlsx"));
        excel.write(fileOutputStream);
        //关闭资源
        excel.close();
        fileOutputStream.close();
    }
}

2.2 读取 Excel 文件

java 复制代码
package com.sky.test;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;

public class POITest {
    public static void main(String[] args) throws IOException {
        read();
    }

    /**
     * 通过POI读取Excel文件
     */
    public static void read() throws IOException{
        FileInputStream fileInputStream = new FileInputStream(new File("D:\\info.xlsx"));
        //创建一个Excel对象,用于读取Excle数据
        XSSFWorkbook excel = new XSSFWorkbook(fileInputStream);
        //读取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);
        }
        //关闭资源
        excel.close();
        fileInputStream.close();
    }
}
相关推荐
她说..8 小时前
Apache Commons Lang3 Pair 完整版实战详解
java·spring·java-ee·apache·springboot
Chengbei118 小时前
VMware Workstation Pro 26H1免费虚拟化利器
运维·安全·web安全·自动化·系统安全·apache·安全架构
充钱大佬2 天前
Python测试基础教程
python·log4j·apache
SelectDB技术团队2 天前
Agent 场景动态 JSON 性能拆解:Apache Doris 比 ClickHouse 快 7 倍、比 Elasticsearch 快 2 倍
数据库·clickhouse·elasticsearch·json·apache·日志分析·apache doris
DolphinScheduler社区2 天前
Apache DolphinScheduler 6 月治理优化,补齐调度运维全链路细节
大数据·运维·云原生·apache·海豚调度
SelectDB技术团队2 天前
AB 实验指标计算场景:Apache Doris / SelectDB 的技术能力、选型对比与实践
大数据·数据库·数据分析·apache·用户运营·apache doris·selectdb
weixin_440058314 天前
2026知识付费小程序:年费全包零抽成,录播题库全功能覆盖
小程序·apache·小程序开发
Apache Flink4 天前
从结构化到多模态:Apache Flink,多模态数据处理的流式底座
大数据·flink·apache
阿里云云原生5 天前
乌镇大赛丨5 万奖金已备好!RocketMQ 等你一起打造全新 AI-Native 管控平台
apache·rocketmq
摇滚侠6 天前
Apache Skywalking 实战 阅读笔记 第三章
笔记·apache·skywalking