Java 如何使用univocity writer将具有列表的对象写入csv文件

Java 如何使用univocity writer将具有列表的对象写入csv文件,java,csv,export-to-csv,Java,Csv,Export To Csv,我有一个javabeana,如下所示 String name; List<Address> address; String city; int pinCode; public class UserVO { @NullString(nulls = { "?", "-" }) @LowerCase @Parsed private String username; @Parsed private List<Address>

我有一个javabean
a
,如下所示

String name;
List<Address> address;
String city;
int pinCode;
public class UserVO {
    @NullString(nulls = { "?", "-" })
    @LowerCase
    @Parsed
    private String username;

    @Parsed
    private List<Address> listOfAddresses;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public List<Address> getListOfAddresses() {
        return listOfAddresses;
    }

    public void setListOfAddresses(List<Address> listOfAddresses) {
        this.listOfAddresses = listOfAddresses;
    }
}
使用univocity bean writer处理器的
@Parsed
注释时,地址字段作为对象写入csv文件。如何打印城市和pincode值

这是我生成CSV文件的代码

File csvFile = File.createTempFile("NEW" + ".csv", "");
Writer out = new FileWriter(csvFile);

FixedWidthFieldLengths lengths = new FixedWidthFieldLengths(30, 20);
FixedWidthWriterSettings settings = new FixedWidthWriterSettings(lengths);

// Creates a BeanWriterProcessor that handles annotated fields in the UserVo class.
settings.setRowWriterProcessor(new BeanWriterProcessor<UserVO>(UserVO.class));

// Sets the file headers
settings.setHeaders("Username", "listOfAddresses");

// Creates a writer with the above settings;
FixedWidthWriter writer = new FixedWidthWriter(out, settings);

// Writes the headers specified in the settings
writer.writeHeaders();

// writes a fixed width row with empty values (new UserVO instance).
writer.processRecord(new UserVO());

// writes values of UserVO
writer.processRecords(userVOs);

writer.close();

response().setContentType("text/csv");
response().setHeader("Content-disposition", "attachment;filename=" + csvFile.getName());
return ok(csvFile);

对于您的情况,您可以创建一个自定义转换类来处理bean中的任意元素。检查以下部分:在注释中使用您自己的转换

使用您自己的示例,我创建了以下自定义转换以打印集合中的对象:

public class CollectionConversion implements Conversion<String, Collection<?>> {

private String separator = "|";

public CollectionConversion(String[] args) {
    if (args.length > 0) {
        separator = args[0];
    }
}

@Override
public Collection<?> execute(String input) {
    return null;
}

//You only need to implement this one when writing.
@Override
public String revert(Collection<?> input) {
    if (input == null) {
        return "";
    }
    StringBuilder out = new StringBuilder();
    for (Object element : input) {
        if (out.length() > 0) {
            out.append(this.separator);
        }
        out.append(element.toString());
    }
    return out.toString();
}
}
现在,在UserVO类中,您可以指定如下地址列表:

public class Address {
    String city;
    int pinCode;

    public String toString() {
        return city + " (" + pinCode + ")";
    }
}
@Convert(conversionClass = CollectionConversion.class, args = { ";" })
@Parsed
private List<Address> listOfAddresses;

我使用同一个univocity编写器将对象写入csv文件。某些数据正在写入为空。在写之前。我正在记录它,我可以看到数据。但一旦完成编写,它就会在csv文件中反映为空

settings.setRowWriterProcessor(new BeanWriterProcessor<Employee>(Employee.class));

writers.writeHeaders("added headers here");

List<Employee> employeeList = service.generatereport(start,enddate);

for(employeel:employeeList) {

//here when i print the values of employeel.getSomethings - i'm getting the value

writer.processRecord(employeel);

//in csv it is null.
}
settings.setRowWriterProcessor(新BeanWriterProcessor(Employee.class));
writers.writeHeaders(“此处添加标题”);
List employeeList=service.generatereport(开始、结束日期);
用于(员工:员工列表){
//在这里,当我打印employeel.getSomethings的值时,我得到了值
过程记录的编写者(employeel);
//在csv中为空。
}

在你写作的地方贴上一些代码CSV@Sarz已发布代码片段。这将是打印集合值的解决方案。但是如果需要将city和pincode值打印为列,那么我们必须在UserVO类中将它们声明为单独的字段谢谢,从某种意义上说是格式,我需要使用univocity的format方法吗?你能回答一下吗?我已经在这里停留了3个小时了。
Username                      listOfAddresses                                   
?                                                                               
user1                         New York (14);Sydney (345)                        
settings.setRowWriterProcessor(new BeanWriterProcessor<Employee>(Employee.class));

writers.writeHeaders("added headers here");

List<Employee> employeeList = service.generatereport(start,enddate);

for(employeel:employeeList) {

//here when i print the values of employeel.getSomethings - i'm getting the value

writer.processRecord(employeel);

//in csv it is null.
}