首页 文章

如何在Apache POI中使用SXSSFWorkbook中的vlookup或公式?

提问于
浏览
2

我是Java开发人员 . 我正在尝试使用Apache POI库的 SXSSFWorkbook 类编写一个大型 xlsx 文件(Excel) .

由于有超过200000行,我没有选择写大文件 . 但我需要使用一些公式或vlookup与外部表 . 我用的时候:

cell.setCellFormula(formulasString);

但它没有工作,它返回0值 .

Q- How to use formulas with SXSSFWorkbook ?

我的代码 -

import java.io.FileOutputStream;
import junit.framework.Assert;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

public class Demo2 
{
    public static void main(String[] args) throws Throwable 
    {
          SXSSFWorkbook wb = new SXSSFWorkbook(100); // keep 100 rows in memory, exceeding rows will be flushed to disk

          FormulaEvaluator fEvaluator=wb.getCreationHelper().createFormulaEvaluator();

          Sheet sh = wb.createSheet();
          for(int rownum = 0; rownum < 100000; rownum++)
          {
               Row row = sh.createRow(rownum);
               for(int cellnum = 0; cellnum < 10; cellnum++)
               {
                  Cell cell = row.createCell(cellnum);
                  String address = new CellReference(cell).formatAsString();
                  cell.setCellValue(address);
               }

               Cell cell1 = row.createCell(12);
               cell1.setCellType(Cell.CELL_TYPE_FORMULA);
               String sF = "SUM(10,20,30)";
               cell1.setCellFormula(sF);
               fEvaluator.clearAllCachedResultValues();

               Cell cell2 = row.createCell(13);
               cell2.setCellValue("Test");
         }

         // Rows with rownum < 900 are flushed and not accessible
         for(int rownum = 0; rownum < 99900; rownum++)
         {
              Assert.assertNull(sh.getRow(rownum));
         }

         // ther last 100 rows are still in memory
         for(int rownum = 99900; rownum < 100000; rownum++)
         {
              Assert.assertNotNull(sh.getRow(rownum));
         }

         FileOutputStream out = new FileOutputStream("sxssf.xlsx");
         wb.write(out);
         out.close();

         // dispose of temporary files backing this workbook on disk
         wb.dispose();
         System.out.println("Done");
    }
}

请尽可能给我建议 . 谢谢 .

3 回答

  • 0

    根据您的问题以及评论中提供的信息,您的问题是您在尝试评估SXSSF单元格时遇到异常的原因 . 作为detailed towards the end of the Formula Evaluation documentation,对于SXSSF公式评估,您需要使用3.13 beta 2或更高版本 .

    还有一个潜在的问题 - 公式评估不仅需要具有公式的单元格在内存中,还需要它所引用的任何其他单元格以及它们所引用的任何单元格 . 这可能会导致问题,具体取决于vlookup尝试调用的区域的宽度 . vlookup所需的所有单元必须在当前窗口中可用,而不是刷新到磁盘 . 因此,您可能需要简化公式和/或增加窗口大小,以确保每个vlookup单元格可以在评估时找到它所依赖的所有单元格的值

  • 0

    不幸的是,SXSSFWorkbook不支持3.12版中的公式 . 我发现这在代码中也是如此,但是,我没有看到任何关于此的官方POI文档 .

    POI documentation for formula evaluation有一些示例代码,用于在创建单元格后评估公式 . 它说它适用于XSSFWorkbook和HSSFWorkbook实现,但没有提到SXSSFWorkbook .

    我试图在示例代码中使用SXSSFWorkbook并收到异常 . 考虑到例外,尽管未在POI文档中明确说明,但SxSSFWorkbook无法评估公式 .

    Unexpected type of cell: class org.apache.poi.xssf.streaming.SXSSFCell. Only XSSFCells can be evaluated.
    
  • 0

    wb.setForceFormulaRecalculation(真);

    在做wb.write(out)之前设置这个你打开文件我excel..excel重新计算你得到计算输出

相关问题