开发语言 Java工具库系列(十八):Apache POI - import org

📅 2026-12-01 👤 拼多多助力营销技巧 📂 拼多多助力2027 👁️ 514 次阅读

Java工具库系列(十八):Apache POI

在Java开发中,处理Microsoft Office文档(如Excel、Word)是一个常见的需求。Apache POI 是一个强大的库,专门用于读写Microsoft Office格式的文件,特别是Excel文件。本文将介绍 Apache POI 的核心功能及其使用方法,帮助你在项目中更高效地处理Office文档。

一、Apache POI 简介

Apache POI(Poor Obfuscation Implementation)是Apache软件基金会的一个项目,提供了API,用于读写基于OLE2和OOXML的Microsoft Office文档。POI的主要组件包括:

POIFS(Poor Obfuscation Implementation File System):处理OLE2文件系统。HSSF(Horrible Spreadsheet Format):处理Excel 97-2003格式。XSSF(XML Spreadsheet Format):处理Excel 2007及更高版本格式。HWPF(Horrible Word Processor Format):处理Word 97-2003格式。XWPF(XML Word Processor Format):处理Word 2007及更高版本格式。

二、Apache POI 的安装

要在项目中使用Apache POI,你需要在项目的构建工具中添加POI的依赖。例如,如果你使用Maven,你可以在 pom.xml 文件中添加以下依赖:

org.apache.poi

poi-ooxml

5.0.0

org.apache.poi

poi

5.0.0

org.apache.xmlbeans

xmlbeans

5.0.2

对于Gradle,你可以添加以下内容到 build.gradle 文件:

implementation 'org.apache.poi:poi-ooxml:5.0.0'

implementation 'org.apache.poi:poi:5.0.0'

implementation 'org.apache.xmlbeans:xmlbeans:5.0.2'

三、Apache POI 的核心功能

1. 处理Excel文件(HSSF和XSSF)

Apache POI 提供了HSSF和XSSF用于处理Excel文件。以下是一些基本操作示例:

创建Excel文件

import org.apache.poi.ss.usermodel.*;

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

import java.io.FileOutputStream;

import java.io.IOException;

public class CreateExcelExample {

public static void main(String[] args) {

Workbook workbook = new XSSFWorkbook();

Sheet sheet = workbook.createSheet("Sheet1");

Row row = sheet.createRow(0);

Cell cell = row.createCell(0);

cell.setCellValue("Hello, Apache POI!");

try (FileOutputStream fos = new FileOutputStream("example.xlsx")) {

workbook.write(fos);

} catch (IOException e) {

e.printStackTrace();

}

}

}

读取Excel文件

import org.apache.poi.ss.usermodel.*;

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

import java.io.FileInputStream;

import java.io.IOException;

public class ReadExcelExample {

public static void main(String[] args) {

try (FileInputStream fis = new FileInputStream("example.xlsx")) {

Workbook workbook = new XSSFWorkbook(fis);

Sheet sheet = workbook.getSheetAt(0);

Row row = sheet.getRow(0);

Cell cell = row.getCell(0);

System.out.println(cell.getStringCellValue()); // 输出 "Hello, Apache POI!"

} catch (IOException e) {

e.printStackTrace();

}

}

}

2. 处理Word文件(HWPF和XWPF)

Apache POI 提供了HWPF和XWPF用于处理Word文件。以下是一些基本操作示例:

创建Word文件

import org.apache.poi.xwpf.usermodel.*;

import java.io.FileOutputStream;

import java.io.IOException;

public class CreateWordExample {

public static void main(String[] args) {

XWPFDocument document = new XWPFDocument();

XWPFParagraph paragraph = document.createParagraph();

XWPFRun run = paragraph.createRun();

run.setText("Hello, Apache POI!");

try (FileOutputStream fos = new FileOutputStream("example.docx")) {

document.write(fos);

} catch (IOException e) {

e.printStackTrace();

}

}

}

读取Word文件

import org.apache.poi.xwpf.usermodel.*;

import java.io.FileInputStream;

import java.io.IOException;

public class ReadWordExample {

public static void main(String[] args) {

try (FileInputStream fis = new FileInputStream("example.docx")) {

XWPFDocument document = new XWPFDocument(fis);

for (XWPFParagraph paragraph : document.getParagraphs()) {

System.out.println(paragraph.getText());

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

3. 操作单元格样式和格式

你可以使用Apache POI设置单元格的样式和格式。

import org.apache.poi.ss.usermodel.*;

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

import java.io.FileOutputStream;

import java.io.IOException;

public class CellStyleExample {

public static void main(String[] args) {

Workbook workbook = new XSSFWorkbook();

Sheet sheet = workbook.createSheet("Sheet1");

// 创建单元格样式

CellStyle style = workbook.createCellStyle();

style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());

style.setFillPattern(FillPatternType.SOLID_FOREGROUND);

Row row = sheet.createRow(0);

Cell cell = row.createCell(0);

cell.setCellValue("Styled Cell");

cell.setCellStyle(style);

try (FileOutputStream fos = new FileOutputStream("styled_example.xlsx")) {

workbook.write(fos);

} catch (IOException e) {

e.printStackTrace();

}

}

}

4. 操作表格和图表

Apache POI 支持在Excel中创建和操作表格和图表。

创建表格

import org.apache.poi.ss.usermodel.*;

import org.apache.poi.xssf.usermodel.*;

import java.io.FileOutputStream;

import java.io.IOException;

public class TableExample {

public static void main(String[] args) {

XSSFWorkbook workbook = new XSSFWorkbook();

XSSFSheet sheet = workbook.createSheet("Sheet1");

for (int i = 0; i < 5; i++) {

Row row = sheet.createRow(i);

for (int j = 0; j < 5; j++) {

Cell cell = row.createCell(j);

cell.setCellValue((i + 1) * (j + 1));

}

}

XSSFTable table = sheet.createTable();

table.setDisplayName("Table1");

table.setCellReferences(new CellRangeAddress(0, 4, 0, 4));

try (FileOutputStream fos = new FileOutputStream("table_example.xlsx")) {

workbook.write(fos);

} catch (IOException e) {

e.printStackTrace();

}

}

}

创建图表

import org.apache.poi.ss.usermodel.*;

import org.apache.poi.xssf.usermodel.*;

import org.apache.poi.ss.util.*;

import org.apache.poi.xssf.usermodel.charts.*;

import java.io.FileOutputStream;

import java.io.IOException;

public class ChartExample { 拼多多帮助力入口

public static void main(String[] args) {

XSSFWorkbook workbook = new XSSFWorkbook();

XSSFSheet sheet = workbook.createSheet("Sheet1");

Row row = sheet.createRow(0);

row.createCell(0).setCellValue("Category");

row.createCell(1).setCellValue("Value");

for (int i = 1; i < 6; i++) {

row = sheet.createRow(i);

row.createCell(0).setCellValue("Category " + i);

row.createCell(1).setCellValue(i);

}

XSSFDrawing drawing = sheet.createDrawingPatriarch();

XSSFClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 7, 10, 20);

XSSFChart chart = drawing.createChart(anchor);

XSSFChartLegend legend = chart.getOrCreateLegend();

legend.setPosition(LegendPosition.TOP_RIGHT);

LineChartData data = chart.getChartDataFactory().createLineChartData();

ChartAxis bottomAxis = chart.getChartAxisFactory().createCategoryAxis(AxisPosition.BOTTOM);

ValueAxis leftAxis = chart.getChartAxisFactory().createValueAxis(AxisPosition.LEFT);

leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);

ChartDataSourcexs = DataSources.fromStringCellRange(sheet, new CellRangeAddress(1, 5, 0, 0));

ChartDataSourceys = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(1, 5, 1, 1));

data.addSeries(xs, ys);

chart.plot(data);

try (FileOutputStream fos = new

FileOutputStream("chart_example.xlsx")) {

workbook.write(fos);

} catch (IOException e) {

e.printStackTrace();

}

}

}

四、总结

Apache POI 是一个功能强大的库,可以帮助开发者在Java项目中处理Microsoft Office文档。无论是创建和读取Excel文件,还是处理Word文档,POI都提供了丰富的API供你使用。希望本文能帮助你更好地理解和使用Apache POI。

下一篇文章将继续探讨Java工具库的其他强大功能,敬请期待!

好文阅读,开发语言

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧