项目作者: jedcua

项目描述 :
Parse CSV or Excel files to DTO
高级语言: Java
项目地址: git://github.com/jedcua/Parseux.git
创建时间: 2017-06-23T04:27:18Z
项目社区:https://github.com/jedcua/Parseux

开源协议:Apache License 2.0

下载


Parseux

Build Status Coverage Status
Code Climate

(NOTE: This project is WIP, its API will change in the future)

Easily parse CSV/Excel files to DTOs

Usage

Suppose you have a csv file

  1. value1,value2
  2. value1,value2
  3. value1,value2
  4. ...

Easily parse that to a DTO by annotating your DTO class with @JsonPropertyOrder, which defines the order of field values to be used for parsing

  1. @JsonPropertyOrder({"field1", "field2"})
  2. class MyDTO {
  3. private String field1;
  4. private String field2;
  5. // Getters and setters...
  6. }

Instantiate CsvAsDTO<>() and retrieve parsed csv as DTO

  1. List<MyDTO> dtos = new CsvAsDTO<>(
  2. csv,
  3. MyDTO.class
  4. ).asDTOs();

csv can be:

  • Stream<String> where each String is 1 row (i.e "value1,value2")
  • Iterator<String> where each String also 1 row
  • BufferedReader
  • InputStreamReader
  • ByteArrayStreamReader
  • byte[]

Parsing Excel

Parsing excel files to DTO is the same with CSV:

  1. List<MyDTO> dtos = new CsvAsDTO<>(
  2. new ExcelIterator(
  3. workbook
  4. )
  5. MyDTO.class
  6. ).asDTOs()

ExcelIterator in an implementation of Iterator<String> that run through all sheets and rows, and iteration of concatenated cells per row as String.

workbook is an instance of XSSFWorkbook from Apache POI

Column Separator

Parsing is comma separated by default. However you can annotate your
DTO class with @ColumnSeparator and provide the delimiter

  1. // Tab separated
  2. @ColumnSeparator(value = '\t')
  3. @JsonPropertyOrder({"field1", "field2"})
  4. class MyDTO { }
  5. // Colon separated
  6. @ColumnSeparator(value = ':')
  7. @JsonPropertyOrder({"field1", "field2"})
  8. class MyDTO2 { }

Installation (Maven)

Add the following on your pom.xml

  1. <dependencies>
  2. <dependency>
  3. <groupId>com.dragonfruit</groupId>
  4. <artifactId>Parseux</artifactId>
  5. <version>0.0.3</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>com.fasterxml.jackson.dataformat</groupId>
  9. <artifactId>jackson-dataformat-csv</artifactId>
  10. <version>2.9.0.pr3</version>
  11. </dependency>
  12. ...
  13. </dependencies>
  14. <repositories>
  15. <repository>
  16. <id>jitpack.io</id>
  17. <url>https://jitpack.io</url>
  18. </repository>
  19. </repositories>

License

Apache Version 2