代码之家  ›  专栏  ›  技术社区  ›  Renardo

Apache POI如何在流模式下使用公式?

  •  2
  • Renardo  · 技术社区  · 9 年前

    我正在使用Apache POI 3.17(当前版本)。当我使用HSSFCell时。setFormula()插入类似“A1+17”的公式。当我在流模式下使用SXSSFCell执行相同操作时。setFormula()公式在输入行中显示(带前导“=”),但单元格中显示的结果始终为0。

    我试着用单元格类型NUMERIC和FORMULA。以下是我的最小无效示例:

     final SXSSFWorkbook wb = new SXSSFWorkbook(); 
     final SXSSFSheet sheet = wb.createSheet("Test-S");
     final SXSSFRow row = sheet.createRow(0);
    
     final SXSSFCell cell1 = row.createCell(0);
     cell1.setCellType(CellType.NUMERIC);
     cell1.setCellValue(124);
    
     final SXSSFCell formulaCell1 = row.createCell(1);
     formulaCell1.setCellType(CellType.FORMULA);
     formulaCell1.setCellFormula("A1 + 17");
    
     final SXSSFCell formulaCell2 = row.createCell(2);
     formulaCell2.setCellType(CellType.NUMERIC);
     formulaCell2.setCellFormula("A1+18");
    
     FileOutputStream os = new FileOutputStream("/tmp/test-s.xlsx");
     wb.write(os);
     wb.close();
     os.close();
    

    如有任何提示,我们将不胜感激。

    3 回复  |  直到 9 年前
        1
  •  4
  •   centic    9 年前

    它适用于我的Excel 2016,当我打开示例文件时,我在单元格中得到了正确的结果。可能旧版本的Excel对此的处理略有不同,请尝试使用以下两个方面强制计算公式

    // evaluate all formulas and store cached results
    wb.getCreationHelper().createFormulaEvaluator().evaluateAll();
    // suggest to Excel to recalculate the formulas itself as well
    sheet.setForceFormulaRecalculation(true);
    

        2
  •  3
  •   Axel Richter    9 年前

    这些答案并没有回答为什么OpenOffice/Libreoffice只有在以下情况下才会出现这个问题 SXSSFCell 用作公式单元格。使用时 XSSFCell 作为公式单元格,它不会发生。

    SXSSFCell 使用单元格值,即使公式根本没有计算。最糟糕的是,如果根本没有计算公式,它使用值0(零)。这是数学中对0值的根本误用。值0显式执行 不 表示没有值或存在未知值。这意味着存在值0,而没有其他值。因此值0应该 用作未计算公式的缓存公式结果。相反 在计算公式之前,应使用值。精确为 XSSFCell 做

    apache poi SXSSFCell 密码

    import java.io.FileOutputStream;
    
    import org.apache.poi.xssf.streaming.*;
    
    import org.apache.poi.ss.usermodel.CellType;
    
    import java.lang.reflect.Field;
    
    import java.util.TreeMap;
    
    public class CreateExcelSXSSFFormula {
    
     public static void main(String[] args) throws Exception {
    
      SXSSFWorkbook wb = new SXSSFWorkbook();
    
      SXSSFSheet sheet = wb.createSheet("Test-S");
      SXSSFRow row = sheet.createRow(0);
    
      SXSSFCell cell = row.createCell(0);
      cell.setCellValue(124);
    
      SXSSFFormulaonlyCell formulacell = new SXSSFFormulaonlyCell(row, 1);
      formulacell.setCellFormula("A1+17");
    
      cell = row.createCell(2);
      cell.setCellFormula("A1+17");
    
      formulacell = new SXSSFFormulaonlyCell(row, 3);
      formulacell.setCellFormula("A1+18");
    
      cell = row.createCell(4);
      cell.setCellFormula("A1+18");
    
      wb.write(new FileOutputStream("test-s.xlsx"));
      wb.close();
      wb.dispose();
    
     }
    
     private static class SXSSFFormulaonlyCell extends SXSSFCell {
    
      SXSSFFormulaonlyCell(SXSSFRow row, int cellidx) throws Exception {
       super(row, CellType.BLANK);
       Field _cells = SXSSFRow.class.getDeclaredField("_cells"); 
       _cells.setAccessible(true);
       @SuppressWarnings("unchecked") //we know the problem and expect runtime error if it possibly occurs
       TreeMap<Integer, SXSSFCell> cells = (TreeMap<Integer, SXSSFCell>)_cells.get(row);
       cells.put(cellidx, this);
      }
    
      @Override
      public CellType getCachedFormulaResultTypeEnum() {
       return CellType.BLANK;
      }
    
     }
    
    }
    
        3
  •  1
  •   Renardo    9 年前

    当然,我应该提到我使用LibreOffice。我现在发现,LibreOffice故意不从Excel创建的表中重新计算公式,它将POI表视为Excel创建的。

    https://ask.libreoffice.org/en/question/12165/calc-auto-recalc-does-not-work/