Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 5694C200CDA for ; Fri, 4 Aug 2017 13:40:56 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 5502516D5AD; Fri, 4 Aug 2017 11:40:56 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 6FED616D5AA for ; Fri, 4 Aug 2017 13:40:55 +0200 (CEST) Received: (qmail 13129 invoked by uid 500); 4 Aug 2017 11:40:54 -0000 Mailing-List: contact user-help@poi.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "POI Users List" Delivered-To: mailing list user@poi.apache.org Received: (qmail 13118 invoked by uid 99); 4 Aug 2017 11:40:53 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd4-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 04 Aug 2017 11:40:53 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd4-us-west.apache.org (ASF Mail Server at spamd4-us-west.apache.org) with ESMTP id 68191C00E7 for ; Fri, 4 Aug 2017 11:40:53 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd4-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 3.032 X-Spam-Level: *** X-Spam-Status: No, score=3.032 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, RCVD_IN_DNSWL_NONE=-0.0001, SPF_FAIL=0.919, URI_HEX=1.313] autolearn=disabled Received: from mx1-lw-us.apache.org ([10.40.0.8]) by localhost (spamd4-us-west.apache.org [10.40.0.11]) (amavisd-new, port 10024) with ESMTP id KwMz_B1szZUG for ; Fri, 4 Aug 2017 11:40:47 +0000 (UTC) Received: from mwork.nabble.com (mwork.nabble.com [162.253.133.43]) by mx1-lw-us.apache.org (ASF Mail Server at mx1-lw-us.apache.org) with ESMTP id 9F5DE5FC4D for ; Fri, 4 Aug 2017 11:40:47 +0000 (UTC) Received: from static.162.255.23.22.macminivault.com (unknown [162.255.23.22]) by mwork.nabble.com (Postfix) with ESMTP id 4FD8358FFB4C1 for ; Fri, 4 Aug 2017 04:40:47 -0700 (MST) Date: Fri, 4 Aug 2017 04:40:47 -0700 (MST) From: Hehabr To: user@poi.apache.org Message-ID: <1501846847333-5728430.post@n5.nabble.com> In-Reply-To: References: <1501753335736-5728410.post@n5.nabble.com> <1501765649405-5728412.post@n5.nabble.com> <1501778244162-5728413.post@n5.nabble.com> <1501832186798-5728427.post@n5.nabble.com> Subject: Re: Apache POI: How to update excel file with many formulas? MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit archived-at: Fri, 04 Aug 2017 11:40:56 -0000 With workbook.setForceFormulaRecalculation(true); is new file correctly saved. All cells have new values! Next problem: bad output for new file. ------------------------------------------------------------------------------------------------- Formula is: C34 cell.getCachedFormulaResultType(): 0 0. case Cell.CELL_TYPE_NUMERIC --> Last evaluated as: 407.2521754511886 Formula is: C34 cell.getCachedFormulaResultType(): 5 5. case Cell.CELL_TYPE_ERROR --> ------------------------------------------------------------------------------------------------- Why CELL_TYPE_ERROR ? All cells have new values in new file... What needs to be changed in code? package quicc.headliner.service; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.DateUtil; 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(); } 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"); } } 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.getCreationHelper().createFormulaEvaluator().evaluateAll(); OutputStream os = new FileOutputStream(excelFileNew); workbook.write(os); os.flush(); os.close(); } catch (Exception e) { e.printStackTrace(); } } } -- View this message in context: http://apache-poi.1045710.n5.nabble.com/Apache-POI-How-to-update-excel-file-with-many-formulas-tp5728410p5728430.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