Return-Path: Delivered-To: apmail-poi-dev-archive@www.apache.org Received: (qmail 43294 invoked from network); 13 Aug 2010 08:56:57 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 13 Aug 2010 08:56:57 -0000 Received: (qmail 28278 invoked by uid 500); 13 Aug 2010 08:56:57 -0000 Delivered-To: apmail-poi-dev-archive@poi.apache.org Received: (qmail 27971 invoked by uid 500); 13 Aug 2010 08:56:53 -0000 Mailing-List: contact dev-help@poi.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "POI Developers List" Delivered-To: mailing list dev@poi.apache.org Received: (qmail 27963 invoked by uid 99); 13 Aug 2010 08:56:53 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 13 Aug 2010 08:56:53 +0000 X-ASF-Spam-Status: No, hits=2.3 required=10.0 tests=FREEMAIL_FROM,SPF_HELO_PASS,SPF_SOFTFAIL,T_TO_NO_BRKTS_FREEMAIL,URI_HEX X-Spam-Check-By: apache.org Received-SPF: softfail (athena.apache.org: transitioning domain of politrons@hotmail.com does not designate 216.139.236.158 as permitted sender) Received: from [216.139.236.158] (HELO kuber.nabble.com) (216.139.236.158) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 13 Aug 2010 08:56:47 +0000 Received: from sam.nabble.com ([192.168.236.26]) by kuber.nabble.com with esmtp (Exim 4.63) (envelope-from ) id 1Ojq3v-0008VF-9z for dev@poi.apache.org; Fri, 13 Aug 2010 01:56:27 -0700 Date: Fri, 13 Aug 2010 01:56:27 -0700 (PDT) From: politrons To: dev@poi.apache.org Message-ID: <1281689787304-2553790.post@n5.nabble.com> Subject: Writing XSSFWorkbook and is empty :-( MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Hi, i'm suffering a wierd behaviour in my code, and i dont understand why. Let me explain you my situation first. I'm reading a xlsx file, i'm saving this buffer in a XSSFWorkBook object. when i read this stream completly, i've create a new XSSFWorkBook for write the contain of the other object(gonna call wbToRead) , this create a n Sheets(depending the sheet number of the wbToRead) for this sheet i create n rows as rows has the wbToRead as well, and for any row i read the original value of the Cell of the wbToRead, i've made a modification and i've save in a new cell that has been created by rowToWrite, so the idea basically as you can imagine is read a xlsx file, modify some values and then save again, but this time, separate every sheet in a independient xlsx. In theory every sheet that i have in my original xlsx now would be a new an modify xlsx, and this is it, but the wierd behaviour is that files has weight 5,6,15 kb, but when i open one the values of cells are completely empty. Any idea guys?. I leave here the code for the code monkeys: public void process(String fileName){ try { XSSFWorkbook wb = new XSSFWorkbook(fileName); readIteration(wb); } catch (IOException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } } private int sheetNumber=0; private String header=""; private String body=""; XSSFWorkbook workbook = null; XSSFSheet sheetToWrite = null; Row rowToWrite = null; private void readIteration(XSSFWorkbook wb){ int sheetNumbers = wb.getNumberOfSheets(); for(int i=0; i < sheetNumbers; i++){ Sheet sheetToRead = wb.getSheetAt(i); workbook = new XSSFWorkbook(); sheetToWrite = workbook.createSheet(); int z=0; for (Iterator rit = sheetToRead.rowIterator(); rit.hasNext(); ) { Row rowToRead = rit.next(); Row rowToWrite = sheetToWrite.createRow(z); body +="\n"; int k =0; for (Iterator cit = rowToRead.cellIterator(); cit.hasNext();) { Cell cellToRead = cit.next(); Cell cellToWrite = rowToWrite.createCell(k++); readValue(cellToRead, cellToWrite); } //rows.put(++z, cells); //TODO:Save as independient xlsx } System.out.print("Sheet Number:" + ++sheetNumber+"\n"); System.out.print(header); System.out.print(body); header=""; body=""; createOutPutSheet(sheetNumber,workbook); } } public final static int CELL_TYPE_NUMERIC = 0; public final static int CELL_TYPE_STRING = 1; public final static int CELL_TYPE_FORMULA = 2; public final static int CELL_TYPE_BLANK = 3; public final static int CELL_TYPE_BOOLEAN = 4; public final static int CELL_TYPE_ERROR = 5; private Cell readValue(Cell cellToRead, Cell cellToWrite){ if(cellToRead.getRowIndex() == 0){ drawHeader(cellToRead,cellToWrite); return cellToRead; } switch (cellToRead.getCellType()){ case CELL_TYPE_NUMERIC: double cellNumericValue = cellToRead.getNumericCellValue(); body = body + "|" + cellNumericValue +""; cellNumericValue=666; cellToWrite.setCellValue(cellNumericValue); break; case CELL_TYPE_STRING: String cellStringValue = cellToRead.getStringCellValue(); body = body + "|" + cellStringValue +""; cellStringValue +="_NEW"; cellToWrite.setCellValue(cellStringValue); break; case CELL_TYPE_FORMULA: String cellFormulaValue = cellToRead.getCellFormula(); body = body + "|" + cellFormulaValue +""; break; case CELL_TYPE_BLANK: //body = body + "|"; break; case CELL_TYPE_BOOLEAN: boolean cellBooleanValue = cellToRead.getBooleanCellValue(); body = body + "|" + cellBooleanValue +""; cellBooleanValue=false; cellToWrite.setCellValue(cellBooleanValue); break; case CELL_TYPE_ERROR: byte error = cellToRead.getErrorCellValue(); body = body + "|" + error +""; break; default: break; } return cellToRead; } private boolean isHeader(Cell cell){ return (cell.getRowIndex() == 1); } private void drawHeader(Cell cellToRead, Cell cellToWrite){ try{ if(cellToRead.getCellType()== Cell.CELL_TYPE_NUMERIC){ double cellNumericValue = cellToRead.getNumericCellValue(); header = header + "|" + cellNumericValue; cellNumericValue=666; cellToWrite.setCellValue(cellNumericValue); }else{ String cellStringValue = cellToRead.getStringCellValue(); header = header + "|" + cellStringValue; cellStringValue +="_NEW"; cellToWrite.setCellValue(cellStringValue); } }catch(Exception e){ System.out.print("" + e); } } private void createOutPutSheet(int sheetNumber, XSSFWorkbook wb){ File file = null; try { FileOutputStream fos = new FileOutputStream("C:\\Excel" + sheetNumber + ".xlsx"); wb.write(fos); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } Thanks a lot -- View this message in context: http://apache-poi.1045710.n5.nabble.com/Writing-XSSFWorkbook-and-is-empty-tp2553790p2553790.html Sent from the POI - Dev mailing list archive at Nabble.com. --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscribe@poi.apache.org For additional commands, e-mail: dev-help@poi.apache.org