java 如何导出excel
使用 Apache POI 库导出 Excel
Apache POI 是一个流行的 Java 库,用于处理 Microsoft Office 格式文件,包括 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 ExcelExporter {
public static void main(String[] args) {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet1");
Row headerRow = sheet.createRow(0);
headerRow.createCell(0).setCellValue("Name");
headerRow.createCell(1).setCellValue("Age");
Row dataRow = sheet.createRow(1);
dataRow.createCell(0).setCellValue("John Doe");
dataRow.createCell(1).setCellValue(30);
try (FileOutputStream outputStream = new FileOutputStream("output.xlsx")) {
workbook.write(outputStream);
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
使用 EasyExcel 库导出 Excel
EasyExcel 是阿里巴巴开源的 Excel 操作工具,性能优异且使用简单。以下是一个示例代码。
import com.alibaba.excel.EasyExcel;
import java.util.ArrayList;
import java.util.List;
public class EasyExcelExporter {
public static void main(String[] args) {
String fileName = "output.xlsx";
List<DemoData> dataList = new ArrayList<>();
dataList.add(new DemoData("John Doe", 30));
dataList.add(new DemoData("Jane Smith", 25));
EasyExcel.write(fileName, DemoData.class).sheet("Sheet1").doWrite(dataList);
}
public static class DemoData {
private String name;
private int age;
public DemoData(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() { return name; }
public int getAge() { return age; }
}
}
导出大数据量的优化
当需要导出大量数据时,可以使用 SXSSFWorkbook(POI 的流式 API)或 EasyExcel 的流式写入功能,避免内存溢出。
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import java.io.FileOutputStream;
import java.io.IOException;
public class LargeExcelExporter {
public static void main(String[] args) {
Workbook workbook = new SXSSFWorkbook(100); // 保留 100 行在内存中
Sheet sheet = workbook.createSheet("Sheet1");
for (int i = 0; i < 10000; i++) {
Row row = sheet.createRow(i);
row.createCell(0).setCellValue("Data " + i);
row.createCell(1).setCellValue(i);
}
try (FileOutputStream outputStream = new FileOutputStream("large_output.xlsx")) {
workbook.write(outputStream);
((SXSSFWorkbook) workbook).dispose(); // 清理临时文件
} catch (IOException e) {
e.printStackTrace();
}
}
}
设置单元格样式
可以通过 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 StyledExcelExporter {
public static void main(String[] args) {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet1");
CellStyle headerStyle = workbook.createCellStyle();
Font headerFont = workbook.createFont();
headerFont.setBold(true);
headerFont.setColor(IndexedColors.RED.getIndex());
headerStyle.setFont(headerFont);
Row headerRow = sheet.createRow(0);
Cell headerCell = headerRow.createCell(0);
headerCell.setCellValue("Header");
headerCell.setCellStyle(headerStyle);
try (FileOutputStream outputStream = new FileOutputStream("styled_output.xlsx")) {
workbook.write(outputStream);
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
以上方法涵盖了从基础到高级的 Excel 导出需求,可以根据具体场景选择合适的工具和优化方式。







