Let's take an example. How would you read all of the data from this spreadsheet?
Yep, you may think of direct reading using Apache POI and a few loops to read comments
and expenses
sections.
Here is how you can archive it with excelorm
(I'm using Lombok
to generate getters/setters):
Simple POJO object:
@Data
public class Invoice {
@Cell("A1")
private String companyName;
@Cell("A2")
private String streetAddress;
@Cell("A3")
private String city;
@Cell("A4")
private String phone;
@Cell("A5")
private String fax;
@Cell("A6")
private String website;
// same mapping for the rest pure fields ..
@CellMark
private Bill billTo;
// will be iterating down to rows until first empty cell (A17, A18 ... n) is found
@CellCollection(cells = "A17", strategy = CellStrategy.ROW_UNTIL_NULL)
private List<Expenses> expenses;
@CellCollection(cells = "A36", strategy = CellStrategy.ROW_UNTIL_NULL)
private List<String> comments;
@Data
public static class Bill {
@Cell("A10")
private String name;
@Cell("A11")
private String companyName;
@Cell("A12")
private String streetAddress;
@Cell("A13")
private String city;
@Cell("A14")
private String phone;
}
@Data
public static class Expenses {
@Cell("A17")
private String description;
@Cell("E17")
private Boolean taxed;
@Cell("F17")
private BigDecimal amount;
}
}
Also we'd need to pass the input stream of target excel document with specified sheet name and POJO class which is Invoice.class
in this case.
InputStream resourceAsStream = ReadViaStreamTest.class.getResourceAsStream("/invoice-template.xlsx");
Invoice invoice = Excelorm.read(resourceAsStream, "Invoice 1", Invoice.class);
System.out.println(invoice);
That's all what you would need to do to retrieve the data! Pretty simple, right?!
- Supported types:
String
,Integer
(int
),Double
(double
),BigDecimal
,Boolean
(boolean
),Float
(float
), enum - Ability to work with collection interfaces such as
List
,Set
,Collection
- Supports for
Map<>
interface as well as using a user-defined object as a value in pair with keyMap<String, UserObject>
- Read rows/columns until a first empty/null cell is found
- Defined a step between rows/columns (works only with
Map
or collections)
@Cell
- use to mark single field to get direct value from the Sheet@CellCollection
- use to construct theSet
(HashSet
) /List
(ArrayList
) /Collection
(ArrayList
)@CellMap
- use to construct theMap
based onHashMap
@CellMark
- Use to mark some user defined object
Excelorm.read(Sheet sheet, Class<E> targetClass)
Excelorm.read(InputStream docInputStream, String sheetName, Class<E> targetClass)