SuperCSV报价每个导出的单元格 [英] SuperCSV quote every exported cell

查看:375
本文介绍了SuperCSV报价每个导出的单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用superCSV将bean数据导出为CSV。



现在,使用CsvPreference.EXCEL_NORTH_EUROPE_PREFERENCE,即使带有特殊字符的单元格也不会被引用。 (使用EXCEL_PREFERENCE设置包含。引号的地址。)



我的目标是以MS Office(匈牙利语)导出地址,电话号码,

解决方案

编辑:可以使用和不会将电话号码转换为+36000000000到36000000000。更新超级CSV 2.1.0



由于超级CSV 2.1.0,您现在可以指定 QuoteMode 首选项以在通常不需要时启用引号。要引用每个列,可以使用内置的 AlwaysQuoteMode 。如果要为特定列启用引号,请使用 ColumnQuoteMode 。请注意,您不能以此方式禁用引用,您必须提供自己的 CsvEncoder






感谢您带来论坛帖子给我注意!看起来像Kasper没有实现该功能。我会看到我可以为即将到来的版本做什么:)



不是黑客超级CSV源添加这个报价一切的功能,你可以扩展超级CSV实现在您自己的项目中。如前所述,如果超级CSV包含特殊字符(引号,逗号等),它只会引用整个字段 - 如果字段包含前导/尾随空格,它也会引用。


$ b $考虑到这一点,没有理由你不能写自己的 Writer ,它覆盖 escapeString()方法(你只需要确保它没有被引用)。

  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){

//执行正常转义
final String转义= super。 escapeString(csvElement);

//如果需要,添加周围的引号
final String quote = String.valueOf((char)preference.getQuoteChar());
if(escaped.startsWith(quote)&&& escaped.endsWith(quote)){
return escaped;
} else {
return quote + escaped + quote;
}
}

}


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.

解决方案

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;
        }
    }

}

这篇关于SuperCSV报价每个导出的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆