代码之家  ›  专栏  ›  技术社区  ›  Mr. Skip

为什么最后一列索引对于ApachePOI中的函数偏移量来说太低,以及如何处理它?

  •  0
  • Mr. Skip  · 技术社区  · 7 年前

    我正在使用ApachePOI进行一些手动计算,最近我遇到了无法使用的问题 OFFSET 从255索引开始的列的公式。

    例如

    XSSFCell cell = sheet.getRow(1).createCell(1);
    cell.setCellFormula("OFFSET(IV220,0,1)");
    
    XSSFFormulaEvaluator evaluator = 
    workbook.getCreationHelper().createFormulaEvaluator();
    
    evaluator.evaluateInCell(cell)
    

    cell 变量总是 #REF! 。此外,请记住 IV220

    cell.setCellFormula("OFFSET(IU220,0,1)");
    

    我发现 org.apache.poi.ss.formula.functions.Offset 具有验证以检查列索引是否长于255。这是因为支持一些过时的文档格式吗?

    我使用的是ApachePOI版本:4.0.0

    1 回复  |  直到 7 年前
        1
  •  2
  •   flopcoder    7 年前

    可能是您正在使用的 .xls 文件,并将创建此类型的文件。但它支持的栏目不多

    但是如果你使用更新的格式 支持1048576行和16384列

    支持相应行和列的MS Excel

    +-----------------+-----------+--------------+---------------------+
    |                 | Max. Rows | Max. Columns | Max. Cols by letter |
    +-----------------+-----------+--------------+---------------------+
    | Excel 365*      | 1,048,576 | 16,384       | XFD                 |
    | Excel 2013      | 1,048,576 | 16,384       | XFD                 |
    | Excel 2010      | 1,048,576 | 16,384       | XFD                 |
    | Excel 2007      | 1,048,576 | 16,384       | XFD                 |
    | Excel 2003      | 65,536    | 256          | IV                  |
    | Excel 2002 (XP) | 65,536    | 256          | IV                  |
    | Excel 2000      | 65,536    | 256          | IV                  |
    | Excel 97        | 65,536    | 256          | IV                  |
    | Excel 95        | 16,384    | 256          | IV                  |
    | Excel 5         | 16,384    | 256          | IV                  |
    +-----------------+-----------+--------------+---------------------+
    

    但这里你用的是 XSSF 这意味着你的文件在 格式,以便支持此限制。但是如果你的文件在 .xls 格式,则不支持此限制。

    如果你使用 你可以试试这个代码。也许对你有帮助。

    private static void writeData() throws IOException {
    
            Workbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = (XSSFSheet) workbook.createSheet();
    
            int r = 0;
            for (int i=0;i<2;i++) {
                Row row = sheet.createRow(r++);
                int column = 0;
                for (int j =0;j<2;j++) {
                    XSSFCell cell = (XSSFCell) row.createCell(column++);
                    if (r == 1 || column == 1) cell.setCellValue(i);
    
                    else if (column == 2) {
                        cell.setCellFormula("OFFSET(IU220,0,1)");
                    }
                }
            }
    
    
            FileOutputStream fileOut = new FileOutputStream("stackProblem.xlsx");
            workbook.write(fileOut);
            workbook.close();
        }
    

    enter image description here

    evaluator.evaluateInCell(cell); 不支持超过255列。你可能有用 evaluator.evaluate(cell);

    试试这个

    public static void main(String[] args) throws IOException {
            writeData();
        }
    
        private static void writeData() throws IOException {
    
            Workbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = (XSSFSheet) workbook.createSheet();
    
    
            XSSFRow row218 = sheet.createRow(218);
            XSSFCell vv2180 = (XSSFCell) row218.createCell(255);
            vv2180.setCellValue(50);
    
            XSSFCell vv2181 = (XSSFCell) row218.createCell(256);
            vv2181.setCellValue(69);
    
            XSSFRow row219 = sheet.createRow(219);
            XSSFCell vv0 = (XSSFCell) row219.createCell(255);
            vv0.setCellValue(40);
    
            XSSFCell vv = (XSSFCell) row219.createCell(256);
            vv.setCellValue(70);
    
            XSSFRow row220 = sheet.createRow(220);
            XSSFCell vv2200 = (XSSFCell) row220.createCell(255);
            vv2200.setCellValue(30);
    
            XSSFCell vv220 = (XSSFCell) row220.createCell(256);
            vv220.setCellValue(20);
    
    
            XSSFCell cell = sheet.createRow(1).createCell(1);
            cell.setCellFormula("OFFSET(IV220,0,1)");
            XSSFFormulaEvaluator evaluator =
                    (XSSFFormulaEvaluator) workbook.getCreationHelper().createFormulaEvaluator();
    
            evaluator.evaluate(cell);
    
    
            FileOutputStream fileOut = new FileOutputStream("stackProblem.xlsx");
            workbook.write(fileOut);
            workbook.close();
        }
    

    poi库的良好参考: apche poi HSSF vs XSSF