views:

222

answers:

1

I'm using Apache POI to create a large Excel spreadsheet that is extremely formula heavy for a client that may later modify the code of my program with new formulas. The big issue that I'm running in to is dealing with the fact that the POI workbooks are 0-indexed for their rows and columns, while the Excel formulas deal with the document as if it was 1-indexed. I'm using a helped class to make the conversions right now:

class RowHelper {
    public static int getCell(int col) {
        return col - 1;
    }

    public static String getCellAddress(int row, int col) {
        return CellReference.convertNumToColString(col) + row;
    }
}

And when I edit rows in the document I write it like this:

posRow.getCell(RowHelper.getCell(189)).setCellFormula(String.format("COUNT(%1$s:%2$s)", RowHelper.getCellAddress(2, 177), RowHelper.getCellAddress(ActiveSheet.getPhysicalNumberOfRows(), 177)));
//=COUNT(FU2:FU477)

But this isn't very clean code and won't be very easy for the client to use later down the road. Is there a better way of doing this?

A: 

While not directly related to your issue, I have found myself wrapping some of the Excel classes with my own to do custom processing. For instance I don't work with the HSSFWorkbooks directly, I work with my ExcelWorkbook class that contains a HSSFWorkbook. The reason I did this was to add relevant helper methods to the class, and for other methods I don't modify, I just pass onto the HSSFWorkbook instance. You could also extend the relevant classes that you are working with to override the behavior, but you might want to look at the source code for them to make sure your not breaking anything.

Casey
Thanks for the suggestion! I ended up creating new classes for each type (JWorkbook, JSheet, etc.) just like you did and implemented the same interface as the originals so I could pass them off as the same objects.
taiidani