开发者

apache poi how to disable external reference or external links?

I've been looking on the web for 30 minutes now and can't find any explanation about that. Here is my problem :

I wrote an application with poi to parse some data from 200 excel files or so and put some of it into a new file. I do some cell evaluation with FormulaEvaluator to know the content of the cells before choosing to keep them or not.

Now, when i test it on a test file with only values in the cells, the program works 开发者_运维知识库perfectly but when i use it on my pile of files I get this error :

"could not resolve external workbook name"

Is there any way to ignore external workbook references or set up the environment so that it wont evaluate formula with external references?

Because the ones I need don't contain references...

Thank you


Can you not just catch the error, and skip over that cell?

You're getting the error because you've asked POI to evaluate a the formula in a cell, and that formula refers to a different file. However, you've not told POI where to find the file that's referenced, so it objects.

If you don't care about cells with external references, just catch the exception and move on to the next cell.

If you do care, you'll need to tell POI where to find your files. You do this with the setupEnvironment(String[],Evaluator[]) method - pass it an array of workbook names, and a matching array of evaluators for those workbooks.


In order for POI to be able to evaluate external references, it needs access to the workbooks in question. As these don't necessarily have the same names on your system as in the workbook, you need to give POI a map of external references to open workbooks, through the setupReferencedWorkbooks(java.util.Map<java.lang.String,FormulaEvaluator> workbooks) method.

I have done please see below code that is working fine at my side

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();
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜