下面是一个使用 Java 和 Apache POI 实现 Excel 导入和导出的简单示例,包括:
- 导出 Excel 文件(写入数据)
- 导入 Excel 文件(读取数据)
环境要求
- Java 8+
- Apache POI 依赖(Maven 示例)
<!-- pom.xml -->
<dependencies>
<!-- Apache POI -->
<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>
</dependencies>
示例代码
1. 导出 Excel(写入数据)
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream;
import java.io.IOException;
public class ExcelExportExample {
public static void main(String[] args) {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("用户信息");
// 表头
Row header = sheet.createRow(0);
header.createCell(0).setCellValue("编号");
header.createCell(1).setCellValue("姓名");
header.createCell(2).setCellValue("年龄");
// 示例数据
Object[][] userData = {
{1, "张三", 25},
{2, "李四", 30},
{3, "王五", 28}
};
int rowNum = 1;
for (Object[] rowData : userData) {
Row row = sheet.createRow(rowNum++);
for (int i = 0; i < rowData.length; i++) {
row.createCell(i).setCellValue(rowData[i].toString());
}
}
// 自动调整列宽
for (int i = 0; i < 3; i++) {
sheet.autoSizeColumn(i);
}
try (FileOutputStream fileOut = new FileOutputStream("用户信息.xlsx")) {
workbook.write(fileOut);
System.out.println("Excel 导出成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
2. 导入 Excel(读取数据)
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
import java.io.IOException;
public class ExcelImportExample {
public static void main(String[] args) {
try (FileInputStream fileIn = new FileInputStream("用户信息.xlsx");
Workbook workbook = new XSSFWorkbook(fileIn)) {
Sheet sheet = workbook.getSheetAt(0);
for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {
Row row = sheet.getRow(rowNum);
int id = Integer.parseInt(row.getCell(0).getStringCellValue());
String name = row.getCell(1).getStringCellValue();
int age = Integer.parseInt(row.getCell(2).getStringCellValue());
System.out.printf("编号: %d, 姓名: %s, 年龄: %d%n", id, name, age);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
注意事项
- Excel 版本使用
.xlsx
,所以用的是XSSFWorkbook
,如果是.xls
,要用HSSFWorkbook
。 - 如果读取的是数值(如
age
),你可以用getNumericCellValue()
代替getStringCellValue()
,要注意单元格格式。
已为你生成一个完整的 Apache POI Excel 导入导出 GUI 示例,功能如下:
- 使用 Swing 提供两个按钮:导出 Excel 和 导入 Excel
- 导出生成
用户信息.xlsx
- 导入时读取该文件并弹出数据显示
✅ 项目结构建议(Maven)
excel-poi-gui/
├── pom.xml
└── src/
└── main/
└── java/
└── ExcelGUI.java ← 上面提供的代码
✅ Maven 依赖示例
<!-- pom.xml -->
<project xmlns="http://maven.apache.org/POM/4.0.0" ...>
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>poi-excel-gui</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
发表回复