DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=30671>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://issues.apache.org/bugzilla/show_bug.cgi?id=30671

inconsistent behavior of HSSFRow.getLastCellNum() OR write function incorrectly marks 
the last cell in the row

           Summary: inconsistent behavior of HSSFRow.getLastCellNum() OR
                    write function incorrectly marks the last cell in the
                    row
           Product: POI
           Version: 2.5
          Platform: PC
        OS/Version: Windows XP
            Status: NEW
          Severity: Critical
          Priority: Other
         Component: HPFS
        AssignedTo: [EMAIL PROTECTED]
        ReportedBy: [EMAIL PROTECTED]


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

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to