3

I'm using superCSV for exporting bean data to CSV. I'd like every cell double quoted, not just the ones with special characters.

Now, using CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE, even the cells with special characters won't get quoted. (With EXCEL_PREFERENCE setting the addresses that contain '.' get quoted.)

My goal is to export addresses, phone numbers, everything in a way that MS Office (Hungarian) can use and i.e. won't convert phone numbers like +36000000000 to 36000000000.

csaadaam
  • 103
  • 1
  • 3
  • 8
  • 1
    I'm a Super CSV developer - I'll take a look at this when I get home from work tonight and get back to you. It should be possible to work around this, but it sounds like a feature that we should add. – James Bassett Apr 17 '12 at 03:34
  • This is the best solution I've found so far http://sourceforge.net/projects/supercsv/forums/forum/718794/topic/2010277 – csaadaam Apr 17 '12 at 08:07
  • 1
    FYI [Super CSV 2.0.0-beta-1](http://supercsv.sourceforge.net/release_notes.html) is out now. It includes many bug fixes and new features (including Maven support and a new Dozer extension for mapping nested properties and arrays/Collections). – James Bassett Sep 18 '12 at 05:43

1 Answers1

3

Edit: Update for Super CSV 2.1.0

Since Super CSV 2.1.0, you can now specify a QuoteMode via the preferences to enable quotes when they're normally not required. To quote every column, you can use the inbuilt AlwaysQuoteMode. If want to enable quotes for particular columns, use the ColumnQuoteMode. Please note that you can't disable quoting this way, you'd have to supply your own CsvEncoder to do that.


Thanks for bringing that forum post to my attention! Looks like Kasper didn't get around to implementing that feature. I'll see what I can do for the upcoming release :)

Instead of hacking the Super CSV source to add this 'quote everything' functionality, you could extend the Super CSV implementation in your own project . As you've mentioned, Super CSV only quotes the whole field if it contains special characters (quote, comma, etc) - it also does it if the field contains leading/trailing spaces.

With this in mind there's no reason you can't write your own Writer which overrides the escapeString() method (you just have to make sure it's not already quoted).

package org.supercsv.io;

import java.io.Writer;

import org.supercsv.prefs.CsvPreference;

public class QuoteAllCsvBeanWriter extends CsvBeanWriter {

    public QuoteAllCsvBeanWriter(Writer writer, CsvPreference preference) {
        super(writer, preference);
    }

    @Override
    protected String escapeString(String csvElement) {

        // perform normal escaping
        final String escaped = super.escapeString(csvElement);

        // add surrounding quotes if required
        final String quote = String.valueOf((char) preference.getQuoteChar());
        if (escaped.startsWith(quote) && escaped.endsWith(quote)){
            return escaped;
        } else {
            return quote + escaped + quote;
        }
    }

}
James Bassett
  • 8,062
  • 3
  • 30
  • 65
  • 1
    Came to almost the same solution, but I like yours better :) (I've checked only if it starts with a quote, and I'm using CsvMapWriter). – csaadaam Apr 17 '12 at 11:33
  • 1
    It's probably overkill, you're right, as the last char has to be a quote if the first one is! – James Bassett Apr 17 '12 at 11:50
  • 1
    @HoundDog when will a new version of SuperCSV being released? It has been 4 years since the last release and there are so many quirks/bugs in 1.52. When looking at SCM activity, I can see only 5 commits this year - should we consider the project as dead? – MRalwasser May 23 '12 at 20:31
  • @MRalwasser No it's very much alive! I've just been flat out at work the last few months. I'm hoping to push a release out in June. Sorry for the delay. – James Bassett May 23 '12 at 23:44