Question

Does anyone know a good library written in java providing a fluid wrapper around Apache POI?

The Ugly Duckling entry on the POI case studies page (last entry) refers to something like this.

Was it helpful?

Solution

I was looking for a DSL wrapper that can generate XLSX files. The Subtlib POI library can only generate XLS (BIFF) files. I forked Subtlib POI to write XLSX files: https://github.com/bekoeppel/poi-ooxml

OTHER TIPS

Searching directly on github, I found subtlib.

Have a look on this repo nambach/ExcelUtil. It supports reading and writing Excel files in declarative way.

public class Main {
    static final Style HEADER_STYLE = Style
        .builder()
        .fontColorInHex("#ffffff")
        .backgroundColorInHex("#191970")
        .border(BorderSide.FULL)
        .horizontalAlignment(HorizontalAlignment.LEFT)
        .build();

    static final Style OTHER_STYLES...

    static final DataTemplate<Book> BOOK_TEMPLATE = DataTemplate
        .fromClass(Book.class)
        .includeFields("isbn", "title", "author")
        .column(c -> c.title("Category")
                      .transform(book -> book.getCategory().getName()))  // derive new column
        .column(c -> c.field("rating")
                      .conditionalStyle(book -> book.getRating() > 4 ?  // styles with conditions
                                                HIGH_RATE : null))
        .config(cf -> cf.startAtCell("A2")
                        .autoSizeColumns(true)
                        .headerStyle(HEADER_STYLE)
                        .dataStyle(DATA_STYLE)
                        .conditionalRowStyle(book -> book.getTitle() // selective styling
                                        .contains("Harry Potter") ? FAVORITE_ONE : null));

    public static void main(String[] args) {
        List<Book> books = dataSource.fetchData();
        InputStream stream = BOOK_TEMPLATE.writeData(books);

        FileUtil.writeToDisk(".../books.xlsx", stream, true);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top