GithubHelp home page GithubHelp logo

poi-wrapper's Introduction

poi-wrapper

A small wrapping layer around POI which makes creating OOXML Excel files with POI more readeable.

Goal

By introducing various object and utility classes, this project aims at :

  1. separating describing styles/font/dataformat from actually creating them
  2. provide a readable way of creating cells and merged region in code (class to a DSL)

This allows factorize, reuse styles/font/border/dataformat in code and drastically reduce code duplication.

These informations can even be static, allowing the use of constants and static imports drastically improving code readibility.

  • Build Status: Build Status

Limitations

This project is available as is and does not provides an extensive support of all styles/font/border/dataformat properties.

It only contains features that I had to use in a project of mine.

That said, the project is open source, feel free to fork or contribute to improve it.

Documentation

The main entry point of using this wrapper is the CellCreator class and its createCell static methods.

With the right static imports, creating a merged region with a border, a specific font and a format can be as consise and readable as the following example.

createCell(region(row, K, 14, L), totalPrice, black11CenterWhite, ALL_MEDIUM_BORDER, CURRENCY);

creates a region spanning from cell 12-K to 14-L, with a medium border all around it, in Arial 11, containing the total price formatted with excel default currency format

Please find below the relevant code requiered to achieve the writting of the code line above. It may seem like a lot of code for a single cell, but there isn't many other lines imports required to create dozens of other cells.

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import static fr.javatronic.poiwrapper.Border.ALL_MEDIUM_BORDER;
import static fr.javatronic.poiwrapper.CellCreator.createCell;
import static fr.javatronic.poiwrapper.DataFormats.CURRENCY;
import static fr.javatronic.poiwrapper.MergedRegionFactory.region;
import static fr.javatronic.poiwrapper.Column.K;
import static fr.javatronic.poiwrapper.Column.L;
import static fr.javatronic.poiwrapper.SheetWrapper.create;
import static org.apache.poi.hssf.usermodel.HSSFFont.FONT_ARIAL;
import static org.apache.poi.ss.usermodel.CellStyle.ALIGN_CENTER;
import static org.apache.poi.ss.usermodel.IndexedColors.BLACK;

[...]
    CellStyleDescriptor black11CenterWhite = CellStyleDescriptor.builder()
      .withFontName(FONT_ARIAL)
      .withFontSize(11)
      .withAlignH(ALIGN_CENTER)
      .withFgColor(BLACK)
      .build();

    Workbook wb = new XSSFWorkbook();
    SheetWrapper sheet = create(wb, "Name of the sheet here");
    Row row = sheet.getOrCreateRow(12);
    float totalPrice = computeTotalPrice();
    createCell(region(row, K, 14, L), totalPrice, black11CenterWhite, ALL_MEDIUM_BORDER, CURRENCY);

[...]

poi-wrapper's People

Contributors

lesaint avatar

Watchers

James Cloos avatar  avatar

poi-wrapper's Issues

move CellCreator#createCell methods to SheetWrapper and delete CellCreator

Moving these methods to SheetWrapper will be a way of saving on having to call SheetWrapper#getOrCreateRow all the time.

In addition, it will save us from the Helper pattern implemented by CellCreator.

Code such as the following :

Row row = sheet.getOrCreateRow(12);
createCell(region(row, K, 14, L), totalPrice, black11CenterWhite, ALL_MEDIUM_BORDER, CURRENCY);

Could be replaced with :

createCell(region(12, K, 14, L), totalPrice, black11CenterWhite, ALL_MEDIUM_BORDER, CURRENCY);

This change requires to change the type of row property of class MergedRegion from a Row to an int.

create a FontDescriptor object

It is not currently possible to factorise Font description when creating multiple CellStyleDescriptor which only difference is the text alignment, for example.

Introducing a FontDescriptor class could help on that part.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.