Java实现导出Excel(Apache POI)

Java可以通过Apache POI库来实现导出Excel文件
超详细的步骤及示例代码:

  1.添加POI依赖:在Maven项目中,需要在pom.xml中添加以下依赖:
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.2</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.2</version>
</dependency>
 2. 创建Excel文件:通过创建Workbook对象来创建Excel文件,支持多种格式,如XLS和XLSX。以下代码创建一个XLSX格式的Excel文件:
// 创建工作簿对象
Workbook workbook = new XSSFWorkbook();
// 创建工作表对象
Sheet sheet = workbook.createSheet("Sheet1");
 3. 创建表头:通过创建Row和Cell对象来创建Excel表头,并设置表头样式:
// 创建表头行
Row headerRow = sheet.createRow(0);
// 创建表头单元格
Cell headerCell = headerRow.createCell(0);
// 设置表头单元格的值
headerCell.setCellValue("姓名");
// 创建样式对象
CellStyle headerStyle = workbook.createCellStyle();
// 设置字体加粗
Font headerFont = workbook.createFont();
headerFont.setBold(true);
headerStyle.setFont(headerFont);
// 设置对齐方式
headerStyle.setAlignment(HorizontalAlignment.CENTER);
headerCell.setCellStyle(headerStyle);
 4. .创建数据行:通过遍历数据列表来创建每一行的数据,并设置单元格样式:
// 创建数据行
int rowIndex = 1;
for (User user : userList) {
    Row row = sheet.createRow(rowIndex++);
    // 创建姓名单元格
    Cell nameCell = row.createCell(0);
    nameCell.setCellValue(user.getName());
    // 创建年龄单元格
    Cell ageCell = row.createCell(1);
    ageCell.setCellValue(user.getAge());
    // 创建样式对象
    CellStyle style = workbook.createCellStyle();
    // 设置对齐方式
    style.setAlignment(HorizontalAlignment.CENTER);
    nameCell.setCellStyle(style);
    ageCell.setCellStyle(style);
}
 5. 输出Excel文件:通过输出流将Workbook对象写入磁盘文件或内存流中。
// 输出Excel文件
FileOutputStream outputStream = new FileOutputStream("userList.xlsx");
workbook.write(outputStream);
outputStream.close();

完整代码:

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

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

public class ExcelExportDemo {

    public static void main(String[] args) throws Exception {
        // 创建数据列表
        List<User> userList = new ArrayList<>();
        userList.add(new User("张三", 20));
        userList.add(new User("李四", 30));

        // 创建工作簿对象
        Workbook workbook = new XSSFWorkbook();
        // 创建工作表对象
        Sheet sheet = workbook.createSheet("Sheet1");

        // 创建表头行
        Row headerRow = sheet.createRow(0);
        // 创建表头单元格
        Cell headerCell = headerRow.createCell(0);
        // 设置表头单元格的值
        headerCell.setCellValue("姓名");
        // 创建样式对象
        CellStyle headerStyle = workbook.createCellStyle();
        // 设置字体加粗
        Font headerFont = workbook.createFont();
        headerFont.setBold(true);
        headerStyle.setFont(headerFont);
        // 设置对齐方式
        headerStyle.setAlignment(HorizontalAlignment.CENTER);
        headerCell.setCellStyle(headerStyle);

        // 创建表头单元格2
        Cell headerCell2 = headerRow.createCell(1);
        // 设置表头单元格2的值
        headerCell2.setCellValue("年龄");
        // 设置样式
        headerCell2.setCellStyle(headerStyle);

        // 创建数据行
        int rowIndex = 1;
        for (User user : userList) {
            Row row = sheet.createRow(rowIndex++);
            // 创建姓名单元格
            Cell nameCell = row.createCell(0);
            nameCell.setCellValue(user.getName());
            // 创建年龄单元格
            Cell ageCell = row.createCell(1);
            ageCell.setCellValue(user.getAge());
            // 创建样式对象
            CellStyle style = workbook.createCellStyle();
            // 设置对齐方式
            style.setAlignment(HorizontalAlignment.CENTER);
            nameCell.setCellStyle(style);
            ageCell.setCellStyle(style);
        }

        // 输出Excel文件
        FileOutputStream outputStream = new FileOutputStream("userList.xlsx");
        workbook.write(outputStream);
        outputStream.close();
    }

    static class User {
        private String name;
        private int age;

        public User(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值