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 6010A200CC1 for ; Mon, 10 Jul 2017 22:48:44 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 5CB961695D5; Mon, 10 Jul 2017 20:48:44 +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 7D90A1695CA for ; Mon, 10 Jul 2017 22:48:43 +0200 (CEST) Received: (qmail 69145 invoked by uid 500); 10 Jul 2017 20:48:42 -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 69133 invoked by uid 99); 10 Jul 2017 20:48:42 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd3-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 10 Jul 2017 20:48:42 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd3-us-west.apache.org (ASF Mail Server at spamd3-us-west.apache.org) with ESMTP id D5F681942AF for ; Mon, 10 Jul 2017 20:48:41 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd3-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 2.242 X-Spam-Level: ** X-Spam-Status: No, score=2.242 tagged_above=-999 required=6.31 tests=[RCVD_IN_DNSWL_NONE=-0.0001, SPF_FAIL=0.919, T_FILL_THIS_FORM_SHORT=0.01, URI_HEX=1.313] autolearn=disabled Received: from mx1-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd3-us-west.apache.org [10.40.0.10]) (amavisd-new, port 10024) with ESMTP id 13lNxT1eAaey for ; Mon, 10 Jul 2017 20:48:39 +0000 (UTC) Received: from mwork.nabble.com (mwork.nabble.com [162.253.133.43]) by mx1-lw-eu.apache.org (ASF Mail Server at mx1-lw-eu.apache.org) with ESMTP id 57FBC6274E for ; Mon, 10 Jul 2017 20:23:59 +0000 (UTC) Received: from static.162.255.23.22.macminivault.com (unknown [162.255.23.22]) by mwork.nabble.com (Postfix) with ESMTP id CE1BF51AA8814 for ; Mon, 10 Jul 2017 13:23:58 -0700 (MST) Date: Mon, 10 Jul 2017 13:23:58 -0700 (MST) From: Hehabr To: user@poi.apache.org Message-ID: <1499718238841-5728177.post@n5.nabble.com> In-Reply-To: References: <1499614367054-5728112.post@n5.nabble.com> Subject: Re: Apache POI : Problem with Excel updating MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit archived-at: Mon, 10 Jul 2017 20:48:44 -0000 package poi.service; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.util.CellReference; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.OutputStream; public class POITestEva { private String excelFilePath = "C:/Test/1.xlsm"; private FileInputStream inputStream; private XSSFWorkbook workbook; public static void main(String[] args) { POITestEva pOITestEva = new POITestEva(); pOITestEva.updateCell(3.0); System.out.println("D5 = " + pOITestEva.readCellTest("D5")); // Line 23 } public void updateCell(Double newData) { try { File excel = new File(excelFilePath); inputStream = new FileInputStream(excel); workbook = new XSSFWorkbook(inputStream); workbook.setForceFormulaRecalculation(true); Cell cell = getCell(1, "C8"); if (cell != null) { cell.setCellValue(newData); } workbook.getCreationHelper().createFormulaEvaluator().evaluateAll(); OutputStream output = new FileOutputStream(excel); workbook.write(output); output.flush(); output.close(); workbook.close(); inputStream.close(); } catch (Exception e) { e.printStackTrace(); } } private Cell getCell(int sheetNr, String cellId) { CellReference ref = new CellReference(cellId); return getCell(sheetNr, ref.getCol(), ref.getRow()); } private Cell getCell(int sheetNr, int col, int row) { XSSFSheet sheet = workbook.getSheetAt(sheetNr); if (sheet.getRow(row) != null && sheet.getRow(row).getCell(col) != null && !(sheet.getRow(row).getCell(col).getCellType() == Cell.CELL_TYPE_BLANK)) { return sheet.getRow(row).getCell(col); } return null; } public Double readCellTest(String cellId) { try { File excel = new File(excelFilePath); inputStream = new FileInputStream(excel); workbook = new XSSFWorkbook(inputStream); Double result = ( (Double) (readCell(cellId)) ); // Line 74 if (workbook != null) { workbook.close(); } if (inputStream != null) { workbook.close(); } return result; } catch (Exception e) { e.printStackTrace(); return null; } } private Object readCell(String cellId) { Cell cell = getCell(1, cellId); return handleCell(cell.getCellType(), cell); // Line 91 } @SuppressWarnings("deprecation") private Object handleCell(int type, Cell cell) { switch (type) { case XSSFCell.CELL_TYPE_STRING: return cell.getStringCellValue(); case XSSFCell.CELL_TYPE_NUMERIC: return cell.getNumericCellValue(); case XSSFCell.CELL_TYPE_BOOLEAN: return cell.getBooleanCellValue(); case XSSFCell.CELL_TYPE_BLANK: return null; case XSSFCell.CELL_TYPE_ERROR: return null; case XSSFCell.CELL_TYPE_FORMULA: return cell.getNumericCellValue(); // Line 109 default: return null; } } } ------------------------------------------------------------------------- "C:\Program Files\Java\jdk1.8.0_51\bin\java" -Didea.launcher.port=... ...java.lang.IllegalStateException: Cannot get a NUMERIC value from a STRING cell D5 = null at org.apache.poi.xssf.usermodel.XSSFCell.typeMismatch(XSSFCell.java:1050) at org.apache.poi.xssf.usermodel.XSSFCell.getNumericCellValue(XSSFCell.java:310) at quicc.headliner.service.POITestEva.handleCell(POITestEva.java:109) at quicc.headliner.service.POITestEva.readCell(POITestEva.java:91) at quicc.headliner.service.POITestEva.readCellTest(POITestEva.java:74) at quicc.headliner.service.POITestEva.main(POITestEva.java:23) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) Process finished with exit code 0 -- View this message in context: http://apache-poi.1045710.n5.nabble.com/Apache-POI-Problem-with-Excel-updating-tp5728112p5728177.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