ExportReportCsv pom添加
时间: 2023-12-14 08:48:17 浏览: 23
要在 Maven 项目中添加导出 CSV 报表的功能,可以按照以下步骤修改 pom.xml 文件:
1. 添加 Apache Commons CSV 依赖
在 `<dependencies>` 标签中添加以下依赖:
```xml
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.8</version>
</dependency>
```
这里使用的是版本号为 1.8 的 Apache Commons CSV 库。
2. 添加 Maven 插件
在 `<build>` 标签中添加以下插件:
```xml
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.example.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
```
这里使用的是 Maven 内置的 maven-jar-plugin 插件,并配置了 Main-Class。
3. 实现导出 CSV 报表的代码
在 Main 类中添加导出 CSV 报表的代码,示例代码如下:
```java
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
String csvFile = "report.csv";
List<String> headers = Arrays.asList("Name", "Age", "Address");
List<List<String>> data = Arrays.asList(
Arrays.asList("Alice", "28", "123 Main St."),
Arrays.asList("Bob", "35", "456 Oak Ave."),
Arrays.asList("Charlie", "42", "789 Maple Blvd.")
);
try {
FileWriter fileWriter = new FileWriter(csvFile);
CSVPrinter csvPrinter = new CSVPrinter(fileWriter, CSVFormat.DEFAULT.withHeader(headers.toArray(new String[0])));
for (List<String> row : data) {
csvPrinter.printRecord(row);
}
csvPrinter.flush();
csvPrinter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
这里使用的是 Apache Commons CSV 提供的 CSVPrinter 类来将数据输出到 CSV 文件中。注意要在代码中指定 CSV 文件的路径、表头和数据。
4. 打包并运行程序
使用 Maven 打包程序并运行,示例命令如下:
```
mvn clean package
java -jar target/my-app-1.0-SNAPSHOT.jar
```
这里假设程序的主类是 com.example.Main。程序运行后,会在项目根目录下生成一个 report.csv 文件,里面包含导出的 CSV 报表数据。