阿帕奇poi。配方评估。 evaluteAll vs setForceFormulaRecalculation

时间:2013-09-11 06:20:35

标签: java apache-poi xssf

enter image description here

[质量更好]: http://imageshack.us/photo/my-images/51/v5cg.png/

Problem:我在workBook.xlsx中使用公式。根据具体情况改变价值(3,3)。在这个例子中,我将10改为20.然后我想计算使用这个单元格的公式。完成后,它将覆盖单元格的值,删除公式并记录该值。此单元格不再用于计算。

Code:

public class ReaderXlsx {
    public List<List<String>> ReaderXlsx(String sfilename, int firstColumn, int columnsCount, int rowsCount, int moduleCount){
        int lastColumn=firstColumn+columnsCount;
        List<List<String>> rowsContent = new ArrayList<List<String>>();
        try ( FileInputStream fileInputStream = new FileInputStream("C:\\Users\\student3\\"+sfilename+".xlsx");)
        {
            XSSFWorkbook workBook = new XSSFWorkbook(fileInputStream);
            XSSFSheet sheet = workBook.getSheetAt(0);
            int firstRow = findOneInFirstColumn(sheet,50);
            setModuleCount(sheet,moduleCount);
            workBook.getCreationHelper().createFormulaEvaluator().evaluateAll();

            toNewLine:
            for (int lineId=firstRow;lineId<rowsCount;lineId++)            {
                List<String> columnsContent = new ArrayList<String>();
                Row row = sheet.getRow(lineId);
                if (row==null){continue toNewLine;}
                if (row.getCell(2)==null || row.getCell(2).getStringCellValue().equals("") ) {continue toNewLine;}
                    for (int columnId=firstColumn+1;columnId<lastColumn;columnId++)     {
                        Cell cell = row.getCell(columnId-1);
                        if ((cell==null)) { continue toNewLine;}
                        cell.setCellType(Cell.CELL_TYPE_STRING);
                        if ((columnId==0 & cell.getStringCellValue().equals(""))) {continue toNewLine;}
                        if ((columnId==5 & cell.getStringCellValue().equals("")) || (columnId==6 & cell.getStringCellValue().equals("")))  cell.setCellValue("0");
                        columnsContent.add(cell.getStringCellValue());
                    }
                rowsContent.add(columnsContent);
            }
            try(FileOutputStream out = new FileOutputStream("C:\\Users\\student3\\Used"+sfilename+".xlsx"); ) {
                workBook.write(out);
                out.close();         }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return rowsContent;
    }
    private Integer findOneInFirstColumn(XSSFSheet sheet, Integer maxRows){
        int result=0;
        toNextLine:
        for (int lineId=0;lineId<maxRows;lineId++){
        Row row = sheet.getRow(lineId);
            if (row==null | row.getCell(0)==null) {result++; continue toNextLine; }
            else{
                row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
                if (row.getCell(0).getStringCellValue().equals("1")){return result;}
                else {result++;}
            }
        }
        return result;
    }
    private void setModuleCount(XSSFSheet sheet, Integer moduleCount){
        Row row = sheet.getRow(1);
        if (moduleCount==0) {}
        if (moduleCount>0) {row.createCell(2).setCellValue(moduleCount.toString());}
    }

}

现在我使用2个文件,因为我需要保存我的公式。我想只使用1(源)并正确更新它。

0 个答案:

没有答案