当前位置: 首页 > news >正文

Apache POI 详解 - Java 操作 Excel/Word/PPT

Apache POI 详解 - Java 操作 Excel/Word/PPT

Apache POI(Poor Obfuscation Implementation)是 Apache 提供的 Java 操作 Microsoft Office 文档(Excel、Word、PPT) 的开源库。它支持 .xls(HSSF)、.xlsx(XSSF)、.doc(HWPF)、.docx(XWPF)、.ppt(HSLF)、.pptx(XSLF)等格式。


1. POI 核心组件

组件功能适用格式
HSSF操作 Excel 97-2003(.xls.xls
XSSF操作 Excel 2007+(.xlsx.xlsx
SXSSF流式处理大 Excel(.xlsx.xlsx
HWPF操作 Word 97-2003(.doc.doc
XWPF操作 Word 2007+(.docx.docx
HSLF操作 PowerPoint 97-2003(.ppt.ppt
XSLF操作 PowerPoint 2007+(.pptx.pptx

2. POI 依赖

Maven 依赖

<!-- Excel 操作(HSSF/XSSF/SXSSF) -->
<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>5.2.3</version>
</dependency>
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>5.2.3</version>
</dependency><!-- Word 操作(XWPF) -->
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-scratchpad</artifactId><version>5.2.3</version>
</dependency>

3. POI 操作 Excel(XSSF/HSSF)

(1)写入 Excel(XSSF - .xlsx

import org.apache.poi.xssf.usermodel.*;
import java.io.FileOutputStream;public class ExcelWriter {public static void main(String[] args) throws Exception {// 1. 创建工作簿XSSFWorkbook workbook = new XSSFWorkbook();// 2. 创建工作表XSSFSheet sheet = workbook.createSheet("Sheet1");// 3. 创建行(第 0 行)XSSFRow row = sheet.createRow(0);// 4. 创建单元格并写入数据row.createCell(0).setCellValue("姓名");row.createCell(1).setCellValue("年龄");// 5. 写入数据行XSSFRow dataRow = sheet.createRow(1);dataRow.createCell(0).setCellValue("张三");dataRow.createCell(1).setCellValue(25);// 6. 保存到文件try (FileOutputStream fos = new FileOutputStream("output.xlsx")) {workbook.write(fos);}System.out.println("Excel 文件生成成功!");}
}

(2)读取 Excel(XSSF - .xlsx

import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import java.io.FileInputStream;public class ExcelReader {public static void main(String[] args) throws Exception {// 1. 加载 Excel 文件FileInputStream fis = new FileInputStream("output.xlsx");XSSFWorkbook workbook = new XSSFWorkbook(fis);// 2. 获取工作表XSSFSheet sheet = workbook.getSheetAt(0);// 3. 遍历行和单元格for (Row row : sheet) {for (Cell cell : row) {switch (cell.getCellType()) {case STRING:System.out.print(cell.getStringCellValue() + "\t");break;case NUMERIC:System.out.print(cell.getNumericCellValue() + "\t");break;default:System.out.print("UNKNOWN\t");}}System.out.println();}workbook.close();}
}

(3)SXSSF(流式处理大 Excel)

适用于大数据量(百万行):

import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFRow;
import java.io.FileOutputStream;public class BigExcelWriter {public static void main(String[] args) throws Exception {// 1. 创建 SXSSFWorkbook(默认 100 行缓存)SXSSFWorkbook workbook = new SXSSFWorkbook();SXSSFSheet sheet = workbook.createSheet("大数据");// 2. 写入 100 万行数据for (int i = 0; i < 1_000_000; i++) {SXSSFRow row = sheet.createRow(i);row.createCell(0).setCellValue("数据-" + i);row.createCell(1).setCellValue(i);}// 3. 保存到文件try (FileOutputStream fos = new FileOutputStream("big-data.xlsx")) {workbook.write(fos);}workbook.dispose(); // 清理临时文件System.out.println("大 Excel 文件生成成功!");}
}

4. POI 操作 Word(XWPF)

(1)写入 Word(.docx

import org.apache.poi.xwpf.usermodel.*;
import java.io.FileOutputStream;public class WordWriter {public static void main(String[] args) throws Exception {// 1. 创建文档XWPFDocument document = new XWPFDocument();// 2. 创建段落XWPFParagraph paragraph = document.createParagraph();XWPFRun run = paragraph.createRun();run.setText("Hello, POI Word!");run.setBold(true);run.setFontSize(16);// 3. 保存到文件try (FileOutputStream fos = new FileOutputStream("output.docx")) {document.write(fos);}System.out.println("Word 文件生成成功!");}
}

(2)读取 Word(.docx

import org.apache.poi.xwpf.usermodel.*;
import java.io.FileInputStream;public class WordReader {public static void main(String[] args) throws Exception {// 1. 加载 Word 文件FileInputStream fis = new FileInputStream("output.docx");XWPFDocument document = new XWPFDocument(fis);// 2. 读取所有段落for (XWPFParagraph paragraph : document.getParagraphs()) {System.out.println(paragraph.getText());}document.close();}
}

5. POI 操作 PPT(XSLF)

(1)写入 PPT(.pptx

import org.apache.poi.xslf.usermodel.*;
import java.io.FileOutputStream;public class PPTWriter {public static void main(String[] args) throws Exception {// 1. 创建 PPTXMLSlideShow ppt = new XMLSlideShow();// 2. 创建幻灯片XSLFSlide slide = ppt.createSlide();// 3. 添加标题XSLFTextBox shape = slide.createTextBox();XSLFTextRun textRun = shape.addNewTextParagraph().addNewTextRun();textRun.setText("Hello, POI PPT!");textRun.setFontSize(24.0);// 4. 保存到文件try (FileOutputStream fos = new FileOutputStream("output.pptx")) {ppt.write(fos);}System.out.println("PPT 文件生成成功!");}
}

(2)读取 PPT(.pptx

import org.apache.poi.xslf.usermodel.*;
import java.io.FileInputStream;public class PPTReader {public static void main(String[] args) throws Exception {// 1. 加载 PPT 文件FileInputStream fis = new FileInputStream("output.pptx");XMLSlideShow ppt = new XMLSlideShow(fis);// 2. 遍历所有幻灯片for (XSLFSlide slide : ppt.getSlides()) {for (XSLFShape shape : slide.getShapes()) {if (shape instanceof XSLFTextShape) {System.out.println(((XSLFTextShape) shape).getText());}}}ppt.close();}
}

6. 常见问题

(1)POI 如何处理日期格式?

CellStyle style = workbook.createCellStyle();
style.setDataFormat(workbook.createDataFormat().getFormat("yyyy-MM-dd"));
cell.setCellValue(new Date());
cell.setCellStyle(style);

(2)POI 如何设置单元格样式?

CellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cell.setCellStyle(style);

(3)POI 如何处理大文件?

  • 使用 SXSSF(Excel)或 Event API(SAX 解析)减少内存占用。

7. 总结

功能API适用格式
Excel 操作HSSF/XSSF/SXSSF.xls/.xlsx
Word 操作HWPF/XWPF.doc/.docx
PPT 操作HSLF/XSLF.ppt/.pptx

POI 是 Java 操作 Office 文档的 最主流库,适用于:

  • 报表导出(Excel)
  • 合同生成(Word)
  • 幻灯片处理(PPT)

建议:

  • 小文件XSSF / XWPF / XSLF
  • 大文件SXSSF(Excel)或 SAX 解析(避免 OOM)
http://www.lryc.cn/news/579519.html

相关文章:

  • docker-compose一键部署全栈项目。springboot后端,react前端
  • 如何将信息从 iPhone 同步到Mac(完整步骤和示意图)
  • mac 电脑安装Homebrew来安装npm与node成功后,安装nvm的流程
  • MySQL 8.0 OCP 1Z0-908 题目解析(19)
  • 标准测试测试数据STDF学习笔记
  • MediaCrawler:强大的自媒体平台爬虫工具
  • Spring Boot 多 ActiveMQ 通道配置与多连接消息发送实战(含完整示例与踩坑记录)
  • Ubuntu 24.04 LTS 服务器配置:安装 JDK、Nginx、Redis。
  • 一体机电脑为何热度持续上升?消费者更看重哪些功能?
  • 关于系统无法找到 arm-linux-gcc 命令,这表明你的环境中尚未安装 ARM 交叉编译工具链。以下是详细的解决方案:(DIY机器人工房)
  • 牛客:HJ16 购物单【01背包】【华为机考】
  • 封装 获取paramsByKey 方法
  • 毕业设计(启智模块化机器人的组装与K5的使用
  • 使用Visual Studio 2022创建CUDA编程项目
  • 车载交换机动态MAC学习和静态MAC绑定如何获取MAC地址表
  • jenkins角色权限
  • 这才叫窗口查询!TDEngine官方文档没讲透的实战玩法
  • 微信小程序41~50
  • 佰力博科技与您探讨压电材料的原理与压电效应的应用
  • C++(std::sort)
  • 【轨物洞见】光伏机器人与组件、支架智能化协同白皮书
  • 如何避免服务器出现故障情况?
  • SPLADE 在稀疏向量搜索中的原理与应用详解
  • 【NLP入门系列四】评论文本分类入门案例
  • ubuntu 6.8.0 安装xenomai3.3
  • lspci查看PCI设备详细信息
  • OpenCV篇——项目(二)OCR文档扫描
  • Rust方法语法:赋予结构体行为的力量
  • ConcurrentHashMap 原理
  • Linux多线程(十二)之【生产者消费者模型】