poi-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Hehabr <heh...@web.de>
Subject Re: Apache POI : delete the cached results from the file
Date Wed, 09 Aug 2017 11:27:47 GMT
I can delete the cached results from the file (after update), but I can't
save new file with new cached results
For that i need to open the Excel in GUI mode. But i don't want to open the
Excel Sheet and recalculation. 

See uploaded excel-file :
1.xlsm <http://apache-poi.1045710.n5.nabble.com/file/n5728460/1.xlsm>  

and code:

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");
        }
    }

}

---------------------------------------------------------------------

Output:

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




--
View this message in context: http://apache-poi.1045710.n5.nabble.com/Apache-POI-delete-the-cached-results-from-the-file-tp5728446p5728460.html
Sent from the POI - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Mime
View raw message