Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ApachePOI:使用许多公式更新现有Excel文件而不进行计算_Apache_Apache Poi - Fatal编程技术网

ApachePOI:使用许多公式更新现有Excel文件而不进行计算

ApachePOI:使用许多公式更新现有Excel文件而不进行计算,apache,apache-poi,Apache,Apache Poi,我正在尝试用许多公式更新现有的Excel文件。 我正在使用ApachePOI3.16 我知道ApachePOIAPI本身提供了计算公式的公式计算器, 但是Apache POI不支持某些公式。 我无法使用evaluate函数(Excel文件包含Apache POI不支持的公式) 我可以从文件中删除缓存结果(更新后),但我不能用新的缓存结果保存新文件。 为此,我需要在GUI模式下打开Excel。但我不想打开Excel表格并重新计算 任何人都可以在不打开Excel的情况下获得解决方案,而不使用Form

我正在尝试用许多公式更新现有的Excel文件。 我正在使用ApachePOI3.16

我知道ApachePOIAPI本身提供了计算公式的公式计算器, 但是Apache POI不支持某些公式。 我无法使用evaluate函数(Excel文件包含Apache POI不支持的公式)

我可以从文件中删除缓存结果(更新后),但我不能用新的缓存结果保存新文件。 为此,我需要在GUI模式下打开Excel。但我不想打开Excel表格并重新计算


任何人都可以在不打开Excel的情况下获得解决方案,而不使用FormulaEvaluator,我将获得单元格的新计算值吗

谢谢和问候

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;

public class POITestRB {

    static String excelFileOrig = "C:/Test/1.xlsm";
    static String excelFileNew = "C:/Test/excelFileNew.xlsm";
    static FileInputStream fis;
    static XSSFWorkbook workbook;

    public static void main(String[] args) throws IOException {

        fis = new FileInputStream(excelFileOrig);
        workbook = new XSSFWorkbook(fis);
        gettingCellContents(workbook, "D5");
        updateCell(workbook, 10.0);
        fis.close();
        workbook.close();

        fis = new FileInputStream(excelFileNew);
        workbook = new XSSFWorkbook(fis);
        gettingCellContents(workbook, "D5");
        fis.close();
        workbook.close();

    }

    public static void updateCell(XSSFWorkbook workbook, Double newData) {
        try {
            XSSFSheet sheet = workbook.getSheetAt(1);
            CellReference ref = new CellReference("C8");
            int row = ref.getRow();
            int col = ref.getCol();
            Cell cell = sheet.getRow(row).getCell(col);
            if (cell != null) {
                cell.setCellValue(newData);
            }

            workbook.getCreationHelper().createFormulaEvaluator().clearAllCachedResultValues();
            workbook.setForceFormulaRecalculation(true);

            cleenCach(workbook);

            OutputStream os = new FileOutputStream(excelFileNew);
            workbook.write(os);
            os.flush();
            os.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void cleenCach(XSSFWorkbook workbook) {
        for (Sheet sheet : workbook) {
            for (Row r : sheet) {
                for (Cell c : r) {
                    if (c.getCellTypeEnum() == CellType.FORMULA) {
                        String temp = c.getCellFormula();
                        c.setCellType(CellType.STRING);
                        c.setCellType(CellType.FORMULA);
                        c.setCellFormula(temp);
                    }
                }
            }
        }
    }


    private static void gettingCellContents(XSSFWorkbook workbook, String cellId) {

        workbook.setForceFormulaRecalculation(true);

        XSSFSheet sheet = workbook.getSheetAt(1);
        CellReference ref = new CellReference(cellId);
        int row = ref.getRow();
        int col = ref.getCol();
        Cell cell = sheet.getRow(row).getCell(col);

        switch (cell.getCellTypeEnum()) {
            case STRING:
                System.out.println(cell.getRichStringCellValue().getString());
                break;
            case NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)) {
                    System.out.println(cell.getDateCellValue());
                } else {
                    System.out.println(cell.getNumericCellValue());
                }
                break;
            case BOOLEAN:
                System.out.println(cell.getBooleanCellValue());
                break;
            case FORMULA:
                System.out.println("Formula is: " + cell.getCellFormula());
                System.out.println("cell.getCachedFormulaResultType(): " + cell.getCachedFormulaResultType());

                switch(cell.getCachedFormulaResultType()) {
                    case Cell.CELL_TYPE_NUMERIC:
                        System.out.println("0. case Cell.CELL_TYPE_NUMERIC --> Last evaluated as: " + cell.getNumericCellValue());
                        break;
                    case Cell.CELL_TYPE_STRING:
                        System.out.println("4. case Cell.CELL_TYPE_STRING --> Last evaluated as \"" + cell.getRichStringCellValue() + "\"");
                        break;
                    case Cell.CELL_TYPE_ERROR:
                        System.out.println("5. case Cell.CELL_TYPE_ERROR --> ");
                        break;
                }
                break;
            case BLANK:
                System.out.println();
                break;
            default:
                System.out.println("default");
        }
    }

}
程序输出:

Formula is: C34
cell.getCachedFormulaResultType(): 0
0. case Cell.CELL_TYPE_NUMERIC --> Last evaluated as: 6.0
Formula is: C34
cell.getCachedFormulaResultType(): 0
0. case Cell.CELL_TYPE_NUMERIC --> Last evaluated as: 0.0
请参阅Excel文件:

为什么不将不支持的任何公式函数的实现返回到Apache POI,然后以正常方式在POI中计算所有公式?“不使用FormulaEvaluator,我将获得cel的新计算值”:尝试
org.magic.claivoyance.Math
。要获得新的计算值,而不进行新的计算。为什么不将不支持的任何公式函数的实现返回到Apache POI,然后以正常方式在POI中计算所有公式?“不使用FormulaEvaluator,我将获得cel的新计算值”:Try
org.magic.claivoyance.Math
。获取新的计算值,而不进行新的计算。