更改Jackcess

时间:2015-07-07 18:08:41

标签: java csv ms-access jackcess

我有一个程序,它将我的访问数据库和导出为CSV。一切都很好,但导入CSV的程序有一些硬编码的正则表达式,无法处理某些数据类型的不同格式。

日期
IS:Fri May 01 01 00:00:00 EDT 2015
需要:2015年5月1日00:00:00

布尔吗
不确定这些字段是否是布尔值但是 IS:TRUE或FALSE
需要:0或1

货币
IS:0
需求:0.00美元

字符串
IS:字符串
需要:"字符串"

阅读完文档后,这一行跳出了我的行,#em;行值是强类型的Java对象。在Jackcess中,列类型由名为DataType的Java枚举表示。" 非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

使用uniVocity-parsers

    Writer output = new StringWriter(); // use a FileWriter for your case

    CsvWriterSettings writerSettings = new CsvWriterSettings(); //many options here, check the tutorial

    ObjectRowWriterProcessor writerProcessor = new ObjectRowWriterProcessor(); // handles rows of objects and conversions to String.
    writerSettings.setRowWriterProcessor(writerProcessor);
    writerSettings.setHeaders("A", "B", "C", "D", "E", "F", "G", "H");

    CsvWriter writer = new CsvWriter(output, writerSettings);

    writerProcessor.convertFields(Conversions.toBoolean("0", "1")).add("C", "H"); // will write "0" and "1" instead of "true" and "false" on column "C" and "H"
    writerProcessor.convertFields(Conversions.toDate("M/d/YYYY HH:mm:ss")).add("A", "E");
    writerProcessor.convertFields(Conversions.formatToBigDecimal("$#0.00")).add("B", "D");

    writer.writeHeaders();
    writer.processRecord(new Date(), BigDecimal.TEN, true, new BigDecimal("999.99"), new Date(), "some text here", null, false);
    writer.processRecord(new Date(), BigDecimal.ZERO, false, null, null, null, "more text here", null);
    writer.close();

    System.out.println(output.toString()); //and here is the result

输出将是:

A,B,C,D,E,F,G,H
7/8/2015 07:09:58,$10.00,0,$999.99,7/8/2015 07:09:58,some text here,,1
7/8/2015 07:09:58,$0.00,1,,,,more text here,

披露:我是这个图书馆的作者。它是开源和免费的(Apache V2.0许可证)。