简体   繁体   English

Apache POI Excel行和列索引

[英]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. 我正在使用Apache POI创建一个大型Excel电子表格,该表格对于客户端来说是极其繁琐的事情,以后可能会使用新公式来修改程序代码。 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. 我遇到的最大问题是处理POI工作簿的行和列为0索引的事实,而Excel公式将文档视为1索引。 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. 虽然与您的问题没有直接关系,但我发现自己用自己的包装了一些Excel类来进行自定义处理。 For instance I don't work with the HSSFWorkbooks directly, I work with my ExcelWorkbook class that contains a HSSFWorkbook. 例如,我不直接使用HSSFWorkbook,而是使用包含HSSFWorkbook的ExcelWorkbook类。 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. 我这样做的原因是向类中添加了相关的帮助器方法,而对于其他未修改的方法,我只是将其传递给HSSFWorkbook实例。 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. 您还可以扩展正在使用的相关类以覆盖该行为,但是您可能希望查看它们的源代码以确保不破坏任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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