Java 和 Apache POI 处理 Excel 文件

一、引言

在企业应用中,Excel 是一种常见的数据存储和交换格式。Java 通过 Apache POI 库可以高效地读取、修改和写入 Excel 文件。本文介绍如何使用 Java 处理 Excel 文件,包括读取、写入和修改数据。

二、环境准备
2.1 安装 Java 开发环境

下载并安装 Java SDK:Oracle JDK 下载

验证安装:

java -version

2.2 添加 Maven 依赖

在 pom.xml 中添加 Apache POI 依赖:

org.apache.poi poi-ooxml 5.2.3

三、代码实现
3.1 读取 Excel 文件

以下示例代码演示如何读取 Excel 文件的内容:

package com.example;

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

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class ReadExcel {

public static void main(String[] args) {
String filePath = "data.xlsx"; // Excel 文件路径
try (FileInputStream fis = new FileInputStream(new File(filePath));
Workbook workbook = new XSSFWorkbook(fis)) {

Sheet sheet = workbook.getSheetAt(0); // 读取第一个工作表

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("未知类型\t");
}
}
System.out.println();
}

} catch (IOException e) {
e.printStackTrace();
}
}
}

运行结果(示例 Excel 文件):

姓名 年龄 成绩
张三 23 85.5
李四 25 90.2

3.2 写入 Excel 文件

以下示例代码演示如何创建并写入 Excel 文件:

package com.example;

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

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

public class WriteExcel {

public static void main(String[] args) {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("学生成绩");

// 创建表头
Row headerRow = sheet.createRow(0);
headerRow.createCell(0).setCellValue("姓名");
headerRow.createCell(1).setCellValue("年龄");
headerRow.createCell(2).setCellValue("成绩");

// 添加数据
Object[][] data = {
{"张三", 23, 85.5},
{"李四", 25, 90.2}
};

int rowNum = 1;
for (Object[] rowData : data) {
Row row = sheet.createRow(rowNum++);
for (int i = 0; i < rowData.length; i++) {
if (rowData[i] instanceof String) {
row.createCell(i).setCellValue((String) rowData[i]);
} else if (rowData[i] instanceof Integer) {
row.createCell(i).setCellValue((Integer) rowData[i]);
} else if (rowData[i] instanceof Double) {
row.createCell(i).setCellValue((Double) rowData[i]);
}
}
}

// 保存 Excel 文件
try (FileOutputStream fos = new FileOutputStream(new File("output.xlsx"))) {
workbook.write(fos);
System.out.println("Excel 文件写入成功!");
} catch (IOException e) {
e.printStackTrace();
}

try {
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

运行结果:生成 output.xlsx 文件,包含以下数据:

姓名 年龄 成绩
张三 23 85.5
李四 25 90.2

3.3 修改 Excel 文件

以下示例代码演示如何修改 Excel 文件中的数据:

package com.example;

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

import java.io.*;

public class ModifyExcel {

public static void main(String[] args) {
String filePath = "output.xlsx";

try (FileInputStream fis = new FileInputStream(new File(filePath));
Workbook workbook = new XSSFWorkbook(fis)) {

Sheet sheet = workbook.getSheetAt(0);

// 修改第一行第二列的数据
Row row = sheet.getRow(1);
if (row != null) {
Cell cell = row.getCell(2);
if (cell != null) {
cell.setCellValue(95.0); // 修改成绩
}
}

// 保存修改后的 Excel 文件
try (FileOutputStream fos = new FileOutputStream(new File(filePath))) {
workbook.write(fos);
System.out.println("Excel 文件修改成功!");
}

} catch (IOException e) {
e.printStackTrace();
}
}
}

运行结果:output.xlsx 文件中 张三 的成绩被修改为 95.0。

四、优化与扩展

支持 CSV 读取:扩展程序支持 CSV 格式文件的读取和写入。

批量处理数据:支持从数据库读取数据并写入 Excel。

格式化 Excel:使用 Apache POI 提供的 CellStyle 进行单元格格式化,如字体加粗、颜色填充等。

posted @ 2025-11-20 23:01  ttocr、com  阅读(244)  评论(0)    收藏  举报