我正在尝试从Oracle检索的某些数据中获取CSV。我只需要编写csv,使用查询结果作为csv列。这是我的代码:
// get data
final List<myDto> dataCsv = myDao.getdata();
StringWriter writer = new StringWriter();
CSVWriter csvWriter = new CSVWriter(writer,';');
List<String[]> result = toStringArray(dataCsv);
csvWriter.writeAll(result);
csvWriter.close();
return Response.ok(result).header("Content-Disposition", "attachment; filename=" + fileName).build();`
显然找不到toStringArray()
。但我要建造它吗?我真的需要它吗?如何编辑编辑以使其正常工作?
答案 0 :(得分:2)
如果您只是按照您提供的链接中的示例,您将会看到他们正在做的事情......
private static List<String[]> toStringArray(List<Employee> emps) {
List<String[]> records = new ArrayList<String[]>();
//add header record
records.add(new String[]{"ID","Name","Role","Salary"});
Iterator<Employee> it = emps.iterator();
while(it.hasNext()){
Employee emp = it.next();
records.add(new String[]{emp.getId(),emp.getName(),emp.getRole(),emp.getSalary()});
}
return records;
}
基本上,您需要构建一个String []列表。每个String []代表CSV的单行数据,数组的每个元素都是一个值。所以,是的,您需要从数据模型构建一个List并将其传递给CSVWriter的writeAll()方法。
列表中的第一个String []是CSV的列标题。后续的String数组就是数据本身。
答案 1 :(得分:1)
Apache Commons CSV库可以帮助您完成读取/写入CSV文件的工作。它处理CSV格式的几种变体,其中包括Oracle使用的一种。
Employee.java
让我们为Employee
上一堂课。
package work.basil.example;
import java.util.Objects;
public class Employee {
public Integer id;
public String name, role;
public Integer salary;
public Employee ( Integer id , String name , String role , Integer salary ) {
Objects.requireNonNull( id ); // etc.
this.id = id;
this.name = name;
this.role = role;
this.salary = salary;
}
@Override
public String toString ( ) {
return "Employee{ " +
"id=" + id +
" | name='" + name + '\'' +
" | role='" + role + '\'' +
" | salary=" + salary +
" }";
}
}
设置另一个类来模仿您的DTO的检索。然后我们写入CSV文件。
很显然它找不到toStringArray()。但是我要建造它吗?我真的需要吗?我必须如何编辑编辑才能使其正常工作?
要回答您的特定问题,没有toStringArray
方法可通过DTO对象的成员变量为CSV创建字段值。
将输入或输出数据与Java对象的成员变量映射的想法是generally known as binding。
有用于Java的完善的绑定库,可以分别用XML和JSON JAXB和JSON-B来绑定对象。从此类XML或JSON文本读取对象时,可以将对象自动写成XML或JSON文本,也可以“重新水化”回对象。
但是对于具有简单库(例如 Apache Commons CSV )的CSV,我们分别为每个对象读写数据的每个字段。传递每个DTO对象成员变量,然后Commons CSV会将这些值写到CSV文本中,并带有任何需要的引号,逗号和escaping。
您可以在下面的代码的此行中看到这一点:
printer.printRecord( e.id , e.name , e.role , e.salary );
EmployeeIo.java
这是整个EmployeeIo.java
文件,其中Io
表示输入输出。
package work.basil.example;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class EmployeeIo {
public static void main ( String[] args ) {
EmployeeIo app = new EmployeeIo();
app.doIt();
}
private void doIt ( ) {
// Mimic a collection of DTO objects coming from the database.
List < Employee > employees = new ArrayList <>( 3 );
employees.add( new Employee( 101 , "Alice" , "Boss" , 11_000 ) );
employees.add( new Employee( 102 , "Bob" , "Worker" , 12_000 ) );
employees.add( new Employee( 103 , "Carol" , "Worker" , 13_000 ) );
Path path = Paths.get( "/Users/basilbourque/Employees.csv" );
this.write( employees , path );
}
public void write ( final List < Employee > employees , final Path path ) {
try ( final CSVPrinter printer = CSVFormat.ORACLE.withHeader( "Id" , "Name" , "Role" , "Salary" ).print( path , StandardCharsets.UTF_8 ) ; ) {
for ( Employee e : employees ) {
printer.printRecord( e.id , e.name , e.role , e.salary );
}
} catch ( IOException e ) {
e.printStackTrace();
}
}
}
运行时,我们生成一个文件。