I am trying to read an Excel file using Apache Camel. For that I have found that we can use the CSV component:
https://camel.apache.org/components/3.14.x/dataformats/csv-dataformat.html
For that I have created a simple route which reads a XLSX file from a folder. Then I am trying to print the content of of that Excel file and write to another folder, but the contents of the file are not getting printed. The route is as below:
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.dataformat.csv.CsvDataFormat;
import org.apache.commons.csv.CSVFormat;
import org.springframework.stereotype.Component;
@Component
public class MyRoute extends RouteBuilder {
public void configure() {
CsvDataFormat csv = new CsvDataFormat();
csv.setFormat(CSVFormat.EXCEL);
from("file://C://work//ei01//source")
.unmarshal(csv)
.log("Reached ${body}")
.marshal(csv)
.to("file://C://work//ei01//destination?fileName=test.xlsx");
}
}
The pom.xml is as below:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>sprng-boot-camel-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sprng-boot-camel-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>3.14.1</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-csv-starter</artifactId>
<version>3.14.1</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
Can you please suggest me what I am doing wrong here?
I suppose you try to read a real Excel file (.xlsx
). That does not work with the CSV data format.
The CSVFormat.EXCEL
format is to read CSV files (.csv
) exported from Excel. Because the CSV format is done quite different from product to product, Camel supports multiple "well-known" CSV formats. One of them is CSVFormat.EXCEL
.
To read the native Excel format (I think it is a ZIP file that contains XML files), you need a library like Apache POI.