Java 导出 Excel 2007 (xlsx) 完整指南
在 Java 中导出 Excel 2007 格式(.xlsx)文件,主要有以下几种常用方式:

使用 Apache POI
Apache POI 是最常用的 Java 操作 Office 文件的库。
添加依赖 (Maven)
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version> <!-- 使用最新稳定版本 -->
</dependency>
基本示例代码
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) {
// 创建工作簿
try (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("部门");
// 添加数据行
Row dataRow1 = sheet.createRow(1);
dataRow1.createCell(0).setCellValue("张三");
dataRow1.createCell(1).setCellValue(28);
dataRow1.createCell(2).setCellValue("技术部");
Row dataRow2 = sheet.createRow(2);
dataRow2.createCell(0).setCellValue("李四");
dataRow2.createCell(1).setCellValue(32);
dataRow2.createCell(2).setCellValue("市场部");
// 自动调整列宽
for (int i = 0; i < 3; i++) {
sheet.autoSizeColumn(i);
}
// 写入文件
try (FileOutputStream fileOut = new FileOutputStream("employees.xlsx")) {
workbook.write(fileOut);
System.out.println("Excel文件导出成功!");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
使用 EasyExcel
EasyExcel 是阿里巴巴开源的 Excel 处理框架,性能更好,内存占用更低。
添加依赖 (Maven)
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.1.1</version> <!-- 使用最新稳定版本 -->
</dependency>
基本示例代码
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.WriteTable;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
public class EasyExcelExportExample {
public static void main(String[] args) {
// 准备数据
List<Employee> dataList = new ArrayList<>();
dataList.add(new Employee("张三", 28, "技术部"));
dataList.add(new Employee("李四", 32, "市场部"));
// 写入Excel
try (FileOutputStream fileOut = new FileOutputStream("employees_easy.xlsx")) {
ExcelWriter excelWriter = EasyExcel.write(fileOut, Employee.class).build();
WriteSheet writeSheet = EasyExcel.writerSheet("员工信息").build();
// 设置表头样式
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
headWriteCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
WriteFont headWriteFont = new WriteFont();
headWriteFont.setFontHeightInPoints((short) 10);
headWriteFont.setBold(true);
headWriteCellStyle.setWriteFont(headWriteFont);
headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
// 设置内容样式
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
// 设置样式策略
HorizontalCellStyleStrategy horizontalCellStyleStrategy =
new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
excelWriter.write(dataList, writeSheet);
// 关闭流
excelWriter.finish();
System.out.println("Excel文件导出成功!");
} catch (Exception e) {
e.printStackTrace();
}
}
// 实体类
public static class Employee {
@ExcelProperty("姓名")
private String name;
@ExcelProperty("年龄")
private Integer age;
@ExcelProperty("部门")
private String department;
// 构造方法、getter和setter省略...
public Employee(String name, Integer age, String department) {
this.name = name;
this.age = age;
this.department = department;
}
}
}
使用 JXL
JXL 是一个较老的库,但仍然有人使用。
添加依赖 (Maven)
<dependency>
<groupId>net.sourceforge.jexcelapi</groupId>
<artifactId>jxl</artifactId>
<version>2.6.12</version>
</dependency>
基本示例代码
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import java.io.File;
import java.io.IOException;
public class JxlExportExample {
public static void main(String[] args) {
try {
// 创建可写的工作簿
WritableWorkbook workbook = Workbook.createWorkbook(new File("employees_jxl.xls"));
// 创建工作表
WritableSheet sheet = workbook.createSheet("员工信息", 0);
// 添加标题
sheet.addCell(new Label(0, 0, "姓名"));
sheet.addCell(new Label(1, 0, "年龄"));
sheet.addCell(new Label(2, 0, "部门"));
// 添加数据
sheet.addCell(new Label(0, 1, "张三"));
sheet.addCell(jxl.write.Number.getNumberCell(1, 1, 28));
sheet.addCell(new Label(2, 1, "技术部"));
sheet.addCell(new Label(0, 2, "李四"));
sheet.addCell(jxl.write.Number.getNumberCell(1, 2, 32));
sheet.addCell(new Label(2, 2, "市场部"));
// 写入并关闭
workbook.write();
workbook.close();
System.out.println("Excel文件导出成功!");
} catch (IOException | WriteException e) {
e.printStackTrace();
}
}
}
性能对比
| 特性 | Apache POI | EasyExcel | JXL |
|---|---|---|---|
| 内存占用 | 较高 | 低 | 中等 |
| 性能 | 一般 | 好 | 一般 |
| 功能丰富度 | 非常丰富 | 较丰富 | 较少 |
| 学习曲线 | 中等 | 简单 | 简单 |
| 社区支持 | 活跃 | 活跃 | 不活跃 |
最佳实践建议
- 对于简单导出:如果数据量不大,可以使用 Apache POI 或 JXL
- 对于大数据量导出:推荐使用 EasyExcel,内存占用更低
- 复杂格式处理:Apache POI 功能最全面
- 项目已有依赖:根据现有技术栈选择,避免引入过多依赖
注意事项
- 确保关闭所有资源(使用 try-with-resources)
- 对于大数据量,考虑使用 SXSSFWorkbook (POI) 或 EasyExcel 的分页写入功能
- 处理日期和数字时注意格式化
- 考虑添加异常处理和日志记录
代码示例都适用于 Excel 2007 (.xlsx) 格式,只需使用相应的类名即可。


