You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Apache Camel中创建Excel(.xlsx)文件并发送至SFTP?

Apache Camel 基于现有POJO生成Excel文件并发送到SFTP的实现方案

第一步:引入依赖

Maven项目中添加以下依赖(适配Camel 3.x/4.x版本,可根据自身使用的Camel版本调整对应依赖版本):

<!-- Camel POI组件,用于Excel处理 -->
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-poi</artifactId>
    <version>你的Camel版本号</version>
</dependency>
<!-- Apache POI OOXML支持,处理XLSX格式 -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.2</version>
</dependency>
<!-- 如需使用SFTP组件,额外引入camel-ftp依赖 -->
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-ftp</artifactId>
    <version>你的Camel版本号</version>
</dependency>

方案一:使用内置ExcelDataFormat实现(轻量场景)

无需改造现有POJO,直接将List<IexPerson>序列化为Excel文件:

import org.apache.camel.component.poi.ExcelDataFormat;

// 初始化Excel数据格式,指定生成XLSX格式,按原有POJO字段顺序配置表头
DataFormat excelFormat = new ExcelDataFormat();
excelFormat.setXlsx(true);
excelFormat.setHeaderColumns(Arrays.asList("Person Name", "Gender"));

from("direct-get-list-of-persons")
    // 修改文件后缀为xlsx
    .setHeader(Exchange.FILE_NAME).simple("Persons_${date:now:yyyyMMdd-HHmmssSSS}_${random(1000,10000000)}.xlsx")
    // 序列化生成Excel
    .marshal(excelFormat)
    // 本地测试可输出到本地目录
    .to("file:///tmp?fileName=${header.CamelFileName}")
    // 如需发送到SFTP,替换上面的to为如下配置,按需调整认证、端口等参数
    // .to("sftp://{SFTP用户名}@{SFTP主机地址}/{目标存储目录}?password={SFTP密码}&fileName=${header.CamelFileName}");

方案二:自定义处理器实现(复杂Excel需求场景)

如果需要配置单元格样式、多Sheet、自定义格式等复杂需求,可通过自定义处理器生成Excel:

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFRow;
import java.io.ByteArrayOutputStream;

from("direct-get-list-of-persons")
    .setHeader(Exchange.FILE_NAME).simple("Persons_${date:now:yyyyMMdd-HHmmssSSS}_${random(1000,10000000)}.xlsx")
    .process(exchange -> {
        List<IexPerson> personList = exchange.getIn().getBody(List.class);
        // 创建工作簿和Sheet
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("人员数据");
        
        // 生成表头
        XSSFRow headerRow = sheet.createRow(0);
        headerRow.createCell(0).setCellValue("Person Name");
        headerRow.createCell(1).setCellValue("Gender");
        
        // 填充数据行
        int rowIndex = 1;
        for (IexPerson person : personList) {
            XSSFRow dataRow = sheet.createRow(rowIndex++);
            dataRow.createCell(0).setCellValue(person.getPersonName());
            dataRow.createCell(1).setCellValue(person.getGender());
            // 其他字段按需填充
        }
        
        // 转换为字节数组写入消息体
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        workbook.write(outputStream);
        workbook.close();
        exchange.getIn().setBody(outputStream.toByteArray());
    })
    // 输出到本地或SFTP,配置同方案一
    .to("file:///tmp?fileName=${header.CamelFileName}");

内容的提问来源于stack exchange,提问作者Aleksandar Grujic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 17:57:01