Skip to content

Refactored the core implementation and added several features. Object mapper for Office formats - Excel files, Spreadsheets, etc.

License

Notifications You must be signed in to change notification settings

funpad/excel-object-mapper

Repository files navigation

Build Statuscodecov

excel-object-mapper

excel-object-mapper is a wrapper java library for Apache POI (Apache POI provides java API to read Microsoft Office Formats). POI APIs are very low level giving acess to all the internals of the file formats.

The aim of this project is to provide easy to use highlevel APIs to read the Office file formats by wrapping the POI APIs. In simple terms, the wrapper APIs would look similar to the Jackson Project for XML and JSON, where the data can be mapped to a JAVA Bean and through the mapper APIs, the file data can directly be read as java objects.

- Note that the library supports only spreadsheets (Excel files).

Include

This library is available in Maven Central.

pom.xml entry details..

<dependency> <groupId>io.github.funpad</groupId> <artifactId>excel-object-mapper</artifactId> <version>2.0.0</version> </dependency> 

To install manually, please check the releases page for available versions and change log.

Dependencies

The current implementation uses POI version 4.1.2.

Usage

Spreadsheets (Excel)

Consider the below sample spreadsheet, where data of employees is present.

NameAgeGenderHeight (mts)Address
Bob32MALE1.8410, Madison, Seattle, WA – 123456
John Doe45MALE2.1
Guiliano CarliniMALE1.78Palo Alto, CA – 43234
Mapping Rows to a Java Bean

Create a java bean and map its properties to the columns using the @SheetColumn annotation. The @SheetColumn annotation can be declared on the Field, as well as its Accessor Methods. Pick any one of them to configure the mapped Column as per convenience.

@SheetpublicclassEmployee{// Pick either field or its accessor methods to apply the Column mapping. ... @SheetColumn("Age") privateIntegerage; ... @SheetColumn("Name") publicStringgetName(){returnname} ... }
Reading Rows as Java Objects

Once a mapped Java Bean is ready, use a Reader to read the file rows as objects. Use XlsReader for .xls files and XlsxReader for .xlsx files.

Reading spreadsheet rows as objects ..

 ... finalFilexlsxFile = newFile("<path_to_file>"); finalXlsReaderreader = newXlsReader(); List<Employee> employees = reader.read(Employee.class, xlsxFile); ...
Writing a collection of objects to file

Currently writing to .xlsx files only is supported

 ... // EmployeesList<Employee> employees = newArrayList<Employee>(); employees.add(newEmployee("1", "foo", 12, "MALE", 1.68)); employees.add(newEmployee("2", "bar", null, "MALE", 1.68)); employees.add(newEmployee("3", "foo bar", null, null, null)); // WriterSpreadsheetWriterwriter = newSpreadsheetWriter("<output_file_path>"); writer.addSheet(Employee.class, employees); writer.write(); ...

Implementation Details

Issues

The known issues are already listed under Issues Section.

Please add there your bugs findings, feature requests, enhancements etc.

About

Refactored the core implementation and added several features. Object mapper for Office formats - Excel files, Spreadsheets, etc.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java100.0%