Bug 30671

Summary: inconsistent behavior of HSSFRow.getLastCellNum() OR write function incorrectly marks the last cell in the row
Product: POI Reporter: Lukasz <lukaszr>
Component: HSSFAssignee: POI Developers List <dev>
Status: RESOLVED DUPLICATE    
Severity: critical CC: cpuidle
Priority: P3    
Version: 2.5-FINAL   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   

Description Lukasz 2004-08-14 22:21:15 UTC
Below is test taken from JUnit test case. 
It consists of:
 Part I - write 4 Strings to xls file, 
 Part II - read 
 Part III - verify the read result. 

The thing is that HSSFRow.getLastCellNum() returns 3 for the previously 
written xls file (written with POI) and the test fails. It would be fine, but 
when you open the file with Excel and save the file then close it (Ctrl-S then 
Alt-F4) and comment out the 'Part I' (write test) and re-run the test (so it 
tests the read part) the HSSFRow.getLastCellNum() returns 4.

So either write function incorrectly marks the last cell in the row, or 
HSSFRow.getLastCellNum() returns different numbers for files written with POI 
and written by Excel.


test method (it was cleared out):

	public void testWrite2Read() throws IOException{
	//PART I - write
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = null;
		HSSFRow row = null;
		HSSFCell cell = null;
		String cellValue;


		sheet = wb.createSheet("CDs");
		wb.setSheetName(0, "CDs", HSSFWorkbook.ENCODING_UTF_16);
		
		//write data
		row = sheet.createRow(0);
		String[] data = new String[]{"Identification 
Number:", "Title:", "Retail price:", "Test:"};
		for (short i = 0; i < data.length; i++){
	    		cellValue = data[i];
		    	cell = row.createCell(i);
		    	cell.setCellValue(cellValue);
	    	}

		//write to outputstream
		ByteArrayOutputStream bOut = new ByteArrayOutputStream();
		wb.write(bOut);
		bOut.close();
		
		assertTrue("size of outputstream", bOut.size()>0);

		//COMMENT THIS FOR SECOND TEST
		//write to file
		FileOutputStream file;
		try {
			file = new FileOutputStream("target/test-
classes/xls_test_files_tmp_files/wr_1.xls");
			file.write(bOut.toByteArray());
			file.flush();
			file.close();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}

		
	//PART II - read
		InputStream is = classLoader.getResourceAsStream
("xls_test_files_tmp_files/wr_1.xls");
		assertNotNull("InputStream for 
xls_test_files_tmp_files/wr_1.xls not null", is);

		wb = null;
		sheet = null;
		
		POIFSFileSystem fs = new POIFSFileSystem(is);
		wb = new HSSFWorkbook(fs);
		sheet = wb.getSheetAt(0);
		Iterator rowIter = sheet.rowIterator();

		String[] readData = null;
		if (rowIter.hasNext()){
	    		row = (HSSFRow) rowIter.next();
		    	System.out.println("last cell: "+row.getLastCellNum());
		    	readData = new String[row.getLastCellNum()];
	    		for(short i = 0; i < row.getLastCellNum(); i++){
		    		cell = row.getCell(i);
			    	System.out.println("cell value: "+ 
cell.getStringCellValue());
			    	readData[i] = cell.getStringCellValue();
	    		}
		}

	//Part III verify
		assertNotNull("result not null", readData);
		assertTrue("result not empty", readData.length > 0);

		assertTrue("compare result length vs. expected result length", 
readData.length == data.length);
		for(short i = 0; i < data.length; i++){
			assertEquals("Should be equal", data[i], readData[i]);
		}
	}

  
PS. Before testing plase update the file paths
Comment 1 Andreas Goetz 2006-09-05 13:11:42 UTC
I believe this is a duplicate of Bug 30671. Both issues have been open for ages.
Comment 2 Andreas Goetz 2006-09-05 13:13:11 UTC
Sorry, meant bug 30635.
Comment 3 Andreas Goetz 2006-09-05 13:15:36 UTC
*** This bug has been marked as a duplicate of 30635 ***

*** This bug has been marked as a duplicate of 30635 ***