简体   繁体   English

使用csvwriter创建时出现严重问题

[英]ahaving issue while creating using csvwriter

public void getPersons() 
{
Session s  = sessionFactory.openSession();
Criteria c = s.createCriteria(Person.class);
c.add(Restrictions.isNotNull("id"));
List<Person> al = c.list();
StringWriter writer = new StringWriter();   
CSVWriter csvWriter = new CSVWriter(writer, ',', '\'');
for (Person person : al) {
System.out.println(person.getName()+"------------------"+al);    
csvWriter.writeAll(person);            // this also not work
}

method to read data and in csv .what should i use here?by using csvWriter.writeAll() want to create ',' seprated file The method writeAll(List) in the type CSVWriter is not applicable for the arguments (Person) 读取数据并在csv中使用的方法。我应该在这里使用什么?通过使用csvWriter.writeAll()来创建“,”分隔文件CSVWriter类型的方法writeAll(List)不适用于参数(人)

Unless Person is a list of string arrays, this won't work. 除非Person是字符串数组的列表,否则这将无法工作。

You need a source for the CSVWriter (a List or Iterable of String[]s), which you can create from your list of Persons: 您需要CSVWriter的源(String []的列表或Iterable),可以从“人员”列表中创建该源:

List<String[]> employees = new ArrayList<String[]>();
for (Person person : al) {
    employees.add(new String[] {person.getEmployeeId(), person.getName(), person.getEtc.()})
}

However you produce it, you can pass this to writeAll: 无论您生成它如何,都可以将其传递给writeAll:

csvWriter.writeAll(employees);

...and close the writer: ...并关闭作者:

csvWriter.close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM