gpt4 book ai didi

java - 带有列标题的 StatefulBeanToCsv

转载 作者:行者123 更新时间:2023-12-02 05:56:55 32 4
gpt4 key购买 nike

我正在使用 opencsv-4.0 编写 csv 文件,我需要在输出文件中添加列标题。

这是我的代码。

public static void buildProductCsv(final List<Product> product,
final String filePath) {

try {

Writer writer = new FileWriter(filePath);

// mapping of columns with their positions
ColumnPositionMappingStrategy<Product> mappingStrategy = new ColumnPositionMappingStrategy<Product>();
// Set mappingStrategy type to Product Type
mappingStrategy.setType(Product.class);
// Fields in Product Bean
String[] columns = new String[] { "productCode", "MFD", "EXD" };
// Setting the colums for mappingStrategy
mappingStrategy.setColumnMapping(columns);

StatefulBeanToCsvBuilder<Product> builder = new StatefulBeanToCsvBuilder<Product>(writer);

StatefulBeanToCsv<Product> beanWriter = builder.withMappingStrategy(mappingStrategy).build();
// Writing data to csv file
beanWriter.write(product);
writer.close();

log.info("Your csv file has been generated!");

} catch (Exception ex) {
log.warning("Exception: " + ex.getMessage());
}

}

以上代码创建一个包含数据的 csv 文件。但它不包括该文件中的列标题。

如何添加列标题以输出 csv?

最佳答案

ColumnPositionMappingStrategy#generateHeader 返回空数组

/**
* This method returns an empty array.
* The column position mapping strategy assumes that there is no header, and
* thus it also does not write one, accordingly.
* @return An empty array
*/
@Override
public String[] generateHeader() {
return new String[0];
}

如果从 BeanToCsv 构建器中删除 MappingStrategy

// replace 
StatefulBeanToCsv<Product> beanWriter = builder.withMappingStrategy(mappingStrategy).build();
// with
StatefulBeanToCsv<Product> beanWriter = builder.build();

它将把 Product 的类成员写入 CSV header

如果您的产品类成员名称是

"productCode", "MFD", "EXD"

这应该是正确的解决方案

否则,添加@CsvBindByName注释

import com.opencsv.bean.CsvBindByName;
import com.opencsv.bean.StatefulBeanToCsv;
import com.opencsv.bean.StatefulBeanToCsvBuilder;

import java.io.FileWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

public class CsvTest {

public static void main(String[] args) throws Exception {
Writer writer = new FileWriter(fileName);

StatefulBeanToCsvBuilder<Product> builder = new StatefulBeanToCsvBuilder<>(writer);
StatefulBeanToCsv<Product> beanWriter = builder.build();

List<Product> products = new ArrayList<>();
products.add(new Product("1", "11", "111"));
products.add(new Product("2", "22", "222"));
products.add(new Product("3", "33", "333"));
beanWriter.write(products);
writer.close();
}

public static class Product {
@CsvBindByName(column = "productCode")
String id;
@CsvBindByName(column = "MFD")
String member2;
@CsvBindByName(column = "EXD")
String member3;

Product(String id, String member2, String member3) {
this.id = id;
this.member2 = member2;
this.member3 = member3;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getMember2() {
return member2;
}

public void setMember2(String member2) {
this.member2 = member2;
}

public String getMember3() {
return member3;
}

public void setMember3(String member3) {
this.member3 = member3;
}
}

}

输出:

"EXD","MFD","PRODUCTCODE"

"111","11","1"

"222","22","2"

"333","33","3"

注意;由于 OpenCSV 库使用 Reflection,类、getter 和 setter 需要公开

关于java - 带有列标题的 StatefulBeanToCsv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56004133/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com