Faxel

Faxel is excel to java mapping library. Using handy annotations, developer no longer need to manually parse excel files.

License

License

Categories

Categories

Net
GroupId

GroupId

pl.net.falcon
ArtifactId

ArtifactId

faxel
Last Version

Last Version

1.1.1
Release Date

Release Date

Type

Type

jar
Description

Description

Faxel
Faxel is excel to java mapping library. Using handy annotations, developer no longer need to manually parse excel files.
Project URL

Project URL

https://github.com/msokolowski1991/Faxel
Source Code Management

Source Code Management

https://github.com/msokolowski1991/Faxel

Download faxel

How to add to project

<!-- https://jarcasting.com/artifacts/pl.net.falcon/faxel/ -->
<dependency>
    <groupId>pl.net.falcon</groupId>
    <artifactId>faxel</artifactId>
    <version>1.1.1</version>
</dependency>
// https://jarcasting.com/artifacts/pl.net.falcon/faxel/
implementation 'pl.net.falcon:faxel:1.1.1'
// https://jarcasting.com/artifacts/pl.net.falcon/faxel/
implementation ("pl.net.falcon:faxel:1.1.1")
'pl.net.falcon:faxel:jar:1.1.1'
<dependency org="pl.net.falcon" name="faxel" rev="1.1.1">
  <artifact name="faxel" type="jar" />
</dependency>
@Grapes(
@Grab(group='pl.net.falcon', module='faxel', version='1.1.1')
)
libraryDependencies += "pl.net.falcon" % "faxel" % "1.1.1"
[pl.net.falcon/faxel "1.1.1"]

Dependencies

compile (1)

Group / Artifact Type Version
org.slf4j : slf4j-api jar 1.7.25

provided (1)

Group / Artifact Type Version
org.apache.poi : poi-ooxml jar 3.6

test (3)

Group / Artifact Type Version
org.spockframework : spock-core jar 1.1-groovy-2.4
org.codehaus.groovy : groovy-all jar 2.4.15
ch.qos.logback : logback-classic jar 1.1.7

Project Modules

There are no modules declared in this project.

Faxel

Faxel is an excel to java object mapper library. By annotating your java classes, You can define how an excel should be represented in Java world. Thank's to faxel you no longer need to manually parse excel to Java!

Maven Dependency

<dependency>
  <groupId>pl.net.falcon</groupId>
  <artifactId>faxel</artifactId>
  <version>1.2.0</version>
</dependency>

Example usage

This section will guide through simple example of how to use Faxel.

Java class model

First of all, we need our java classes. Imagine a simple excel containing people. We can annotate it like this:

class PersonDataExcel {
    @ExcelSheet(sheetName = "Person", startPosition = 1, maxPosition = Integer.MAX_VALUE, arrangement = DataArrangementType.ROW)
    private Collection<Person> people;
}

public class Person {
    @Cell(index = 0)
    private String firstName;

    @Cell(index = 1)
    private String lastName;
}

As you can see, first we've annotated a collection of Person as @ExcelSheet. This means that sheet named "Person", starting from position 1 to Integer.MAX_VALUE, will be parsed row by row. The startPosition should be less than maxPosition. The arrangement could be one of ROW or COLUMN.

Model Definition

Next step is to actually parse our excel to previously defined Java model. First we need to create ModelDefinition from our class.

    ModelDefinition model = ModelDefinitionFactory.get().create(PersonDataExcel.class);

This model can be reused as many times as you need. Next step is to get a SourceExcel.

SourceExcel

A SourceExcel class is abstraction over actual parsing library implementation. You can obtain it using SourceFactory which will determine your runtime parsing library:

    InputStream excelStream = getClass().getResourceAsStream("/person-data.xlsx");
    SourceExcel source = SourceFactory.get().create(excelStream);

pass parsing library by enum

    InputStream excelStream = getClass().getResourceAsStream("/person-data.xlsx");
    SourceExcel source = SourceFactory.of(SourceType.DOCX4J_V6).create(excelStream);

or create it directly:

    InputStream excelStream = getClass().getResourceAsStream("/person-data.xlsx");
    Workbook workbook = WorkbookFactory.create(excelStream)
    SourceExcel source = new ApachePoi3Source(workbook);

Apache POI 3.(6+) and Docx4j 6.x.x+ are supported

Parsing excel

Then finally we can fill our model using SourceExcel. The source is used by previously created ModelDefinition to actually parse an excel:

    PersonDataExcel result = model.fill(source, new PersonDataExcel());

The result model is actually the same as argument so you can ignore result and use argument reference.

That's it! We successfully parsed an entire excel!

Types

Built in types support

By default Faxel support following types:

  • String
  • Integer/int
  • Long/long
  • Short/short
  • Float/float
  • Double/double
  • Boolean/boolean
  • Date
  • LocalDate
  • LocalDateTime
  • LocalTime

Custom converter

If you like to parse cell to type Faxel does not support out of the box, you may create your own converter by implementing Converter interface:

    faxel.converter.ColumnConverter

Then use it in @Cell annotation

    @Cell(index = 5, converter = CustomBigDecimalConverter.class)

Release info

1.2.0

  • Added support of docx4j 6.x.x
  • Introduced SourceType enum.
  • SourceFactory can be created using SourceType enum

1.1.1

  • Fixed issue with column arrangement indexes
  • Fixed issue with incorrect data read from cell

1.1.0

  • Fixed issue with @ExcelSheet field type. Now any child of java.util.Collection is supported.
  • Added few helper methods to *Source classes.
  • Internal code refactoring

1.0.0

  • First Release version

Contact

You may contact me at: [email protected]

Versions

Version
1.1.1
1.1.0
1.0.0