更改 Jackcess 的数据类型定义
Changing datatype definition for Jackcess
我构建了一个程序,它获取我的访问数据库并导出为 CSV。一切正常,但接受导出的 CSV 的程序有一些硬编码的正则表达式,无法处理某些数据类型的不同格式。
日期
IS:5 月 1 日星期五 00:00:00 EDT 2015
需求:5/1/2015 00:00:00
布尔值?
不确定这些字段是否为布尔值但
是:真或假
需要:0 或 1
货币
是:0
需要:0.00 美元
字符串
是:字符串
需要:"string"
通读文档后,我突然想到了这一行“行值是强类型 Java 对象。在 Jackcess 中,列类型由 Java 表示名为 DataType 的枚举。"
非常感谢任何帮助。
为此使用 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,.00,0,9.99,7/8/2015 07:09:58,some text here,,1
7/8/2015 07:09:58,[=11=].00,1,,,,more text here,
披露:我是这个图书馆的作者。它是开源且免费的(Apache V2.0 许可证)。
我构建了一个程序,它获取我的访问数据库并导出为 CSV。一切正常,但接受导出的 CSV 的程序有一些硬编码的正则表达式,无法处理某些数据类型的不同格式。
日期
IS:5 月 1 日星期五 00:00:00 EDT 2015
需求:5/1/2015 00:00:00
布尔值?
不确定这些字段是否为布尔值但
是:真或假
需要:0 或 1
货币
是:0
需要:0.00 美元
字符串
是:字符串
需要:"string"
通读文档后,我突然想到了这一行“行值是强类型 Java 对象。在 Jackcess 中,列类型由 Java 表示名为 DataType 的枚举。" 非常感谢任何帮助。
为此使用 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,.00,0,9.99,7/8/2015 07:09:58,some text here,,1
7/8/2015 07:09:58,[=11=].00,1,,,,more text here,
披露:我是这个图书馆的作者。它是开源且免费的(Apache V2.0 许可证)。