简体   繁体   中英

Apache POI Excel Row & Column Indexing

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM