apache poi如何禁用外部引用或外部链接?

时间:2011-07-07 19:31:04

标签: reference external apache-poi

我现在在网上看了30分钟,找不到任何解释。这是我的问题:

我用poi编写了一个应用程序来解析200个excel文件中的一些数据,然后把它们放入一个新文件中。我在使用FormulaEvaluator进行一些细胞评估,以便在选择保留细胞之前了解细胞的含量 现在,当我在一个只有单元格中的值的测试文件上测试它时,程序运行正常,但是当我在我的文件堆上使用它时,我得到了这个错误:

“无法解析外部工作簿名称”

有没有办法忽略外部工作簿引用或设置环境,以便它不会使用外部引用来评估公式?
因为我需要的不包含引用......

谢谢

2 个答案:

答案 0 :(得分:0)

你能不能抓住错误,跳过那个单元格?

您收到错误是因为您已要求POI评估单元格中的公式,并且该公式引用了不同的文件。但是,你没有告诉POI在哪里找到被引用的文件,所以它反对。

如果您不关心具有外部引用的单元格,只需捕获异常并转到下一个单元格。

如果你关心,你需要告诉POI在哪里找到你的文件。您使用setupEnvironment(String[],Evaluator[])方法执行此操作 - 将一系列工作簿名称传递给它,并为这些工作簿分配匹配的计算器数组。

答案 1 :(得分:0)

为了使POI能够评估外部参考,它需要访问有问题的工作簿。由于这些名称在系统上不一定与工作簿中的名称相同,因此您需要通过setupReferencedWorkbooks(java.util.Map 工作簿)方法。

我已经完成了,请参见下面的代码,该代码对我而言很正常

public static void writeWithExternalReference(String cellContent, boolean isRowUpdate, boolean isFormula)
    {
        try
        {
        File yourFile = new File("E:\\Book1.xlsx");
        yourFile.createNewFile(); 
        FileInputStream myxls = null;

        myxls = new FileInputStream(yourFile);

        XSSFWorkbook workbook = new XSSFWorkbook(myxls);
        FormulaEvaluator mainWorkbookEvaluator = workbook.getCreationHelper().createFormulaEvaluator();

            XSSFWorkbook workbook1 = new XSSFWorkbook(new File("E:\\elk\\lookup.xlsx"));

        // Track the workbook references
            Map<String,FormulaEvaluator> workbooks = new HashMap<String, FormulaEvaluator>();
            workbooks.put("Book1.xlsx", mainWorkbookEvaluator);
            workbooks.put("elk/lookup.xlsx", workbook1.getCreationHelper().createFormulaEvaluator());
workbook2.getCreationHelper().createFormulaEvaluator());
// Attach them
            mainWorkbookEvaluator.setupReferencedWorkbooks(workbooks);

            XSSFSheet worksheet = workbook.getSheetAt(0);
            XSSFRow row = null;
            if (isRowUpdate) {
                int lastRow = worksheet.getLastRowNum();
                row = worksheet.createRow(++lastRow);
            }
            else {
                row = worksheet.getRow(worksheet.getLastRowNum());
            }
            if (!isFormula) {
                Cell cell = row.createCell(row.getLastCellNum()==-1 ? 0 : row.getLastCellNum());
                cell.setCellValue(Double.parseDouble(cellContent));
            } else {
                XSSFCell cell  = row.createCell(row.getLastCellNum()==-1 ? 0 : row.getLastCellNum());
                System.out.println(cellContent);
                cell.setCellFormula(cellContent);
                mainWorkbookEvaluator.evaluateInCell(cell);
                cell.setCellFormula(cellContent);
//                mainWorkbookEvaluator.evaluateInCell(cell);
                //System.out.println(cell.getCellFormula() + " = "+cell.getStringCellValue());
            }

            workbook1.close();
            myxls.close();
            FileOutputStream output_file =new FileOutputStream(yourFile,false);
            //write changes
            workbook.write(output_file);
            output_file.close();


        } catch (Exception e) {
            e.printStackTrace();
        }
    }