Mik378
Mik378

Reputation: 22171

known API to write Bean/ResultSet into CSV file

I would like to export a Java Bean or ResultSet(JDBC) into a CSV file through Reflection mechanism.

I have seen this api :

http://opencsv.sourceforge.net/apidocs/au/com/bytecode/opencsv/bean/BeanToCsv.html

but it's not released yet.

Also, it will be fine if we can set some filters to avoid to map some precised fields.

Do you know a known API which owns these features ?

Upvotes: 1

Views: 1370

Answers (3)

Pascal_J
Pascal_J

Reputation: 1

Adding to Kennets answer:

I implemented two classes: One for the header (if needed) and one for the body (actual data)

HEADER

  • The header style class needs to extend ToStringStyle
  • Invoke toString with a single element, e.g. ReflectionToStringBuilder.toString(firstElement, headerStyle)

Constructor:

this.setUseClassName(false);
this.setUseIdentityHashCode(false);
this.setContentStart("");
this.setUseFieldNames(true);
this.setFieldNameValueSeparator("");
this.setContentEnd("\n");

Override Method:

@Override
public void append(StringBuffer buffer, String fieldName, Object value, Boolean fullDetail) {
    super.append(buffer, fieldName, "", fullDetail);
}

BODY

  • The body class needs to extend RecursiveToStringStyle
  • Invoke toString with an array, e.g. ReflectionToStringBuilder.toString(array, bodyStyle)

Constructor:

this.setUseClassName(false);
this.setUseIdentityHashCode(false);
this.setContentStart("");
this.setUseFieldNames(false);
this.setContentEnd("");
this.setNullText("n.a.");
this.setArrayStart("");
this.setArrayEnd("");
this.setArraySeparator("\n");

Override Method:

@Override
public void append(StringBuffer buffer, String fieldName, Object value, Boolean fullDetail) {

    String csvField = Optional.ofNullable(value)
            .map(Objects::toString)
            .map(this::escapeLineBreak)
            .map(this::escapeDoubleQuote)
            .map(this::escapeField)
            .orElse(null);

    super.append(buffer, fieldName, csvField, fullDetail);
}

Formatting Methods:

private String escapeDoubleQuote(final String field) {
    return field.replace("\"", "\"\"");
}

private String escapeLineBreak(final String field) {
    return field.replaceAll("\\R", " ");
}

private String escapeField(final String field) {
    return "\"" + field + "\"";
}

Upvotes: 0

steelshark
steelshark

Reputation: 644

You can just write out to a csv file as you would to a normal .txt file by using an outputstream or so. If you need more advanced excel like stuff I recommend using Apache POI. It has always done the job nice & clean for me.

Upvotes: 0

Kennet
Kennet

Reputation: 5796

Unless there are some ready-made API:s I would use Apache commons http://commons.apache.org/lang/api-2.4/org/apache/commons/lang/builder/ReflectionToStringBuilder.html to get a String representation of an JavaBean. By setting your own ToStringStyle it would be possible to create a CSV style String. There are many possible settings for styling of the String, including excluding fields and so on. And then of course writing it to a file.

Upvotes: 1

Related Questions