Bug 39392 - Error in subsequent calls HSSFDataFormat.getFormat()
Summary: Error in subsequent calls HSSFDataFormat.getFormat()
Status: RESOLVED WONTFIX
Alias: None
Product: POI
Classification: Unclassified
Component: HSSF (show other bugs)
Version: 3.0-dev
Hardware: Other Windows 2000
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-04-24 12:27 UTC by Zdenek Hrib
Modified: 2009-04-04 06:19 UTC (History)
0 users



Attachments
example source (1.62 KB, text/plain)
2006-04-24 12:29 UTC, Zdenek Hrib
Details
compile and run batch file (165 bytes, application/octet-stream)
2006-04-24 12:30 UTC, Zdenek Hrib
Details
example result workbook file (4.00 KB, application/vnd.ms-excel)
2006-04-24 12:31 UTC, Zdenek Hrib
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Zdenek Hrib 2006-04-24 12:27:42 UTC
If the following code is executed, subsequent calls to dataFormat.getFormat() 
will cause all the formated cells to have the last requested format instead of 
the format that was set on them in previous call to dataFormat.getFormat() 


        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("new sheet");

        HSSFRow row = sheet.createRow((short)0);

        short iFormat = 0;

        HSSFDataFormat dataFormat = wb.createDataFormat(); 

        HSSFCell cell = row.createCell((short)0);
        iFormat = dataFormat.getFormat("000000%");
        cell.getCellStyle().setDataFormat(iFormat);         
        cell.setCellFormula("1/"+ String.valueOf(iFormat));
        //cell.setCellValue("1");


        //HSSFCell first_cell = cell;
        
        cell = row.createCell((short)1);
        iFormat = dataFormat.getFormat("00000%");
        cell.getCellStyle().setDataFormat(iFormat);         
        cell.setCellFormula("1/"+ String.valueOf(iFormat));
        //cell.setCellValue("1");


        cell = row.createCell((short)2);
        iFormat = dataFormat.getFormat("0000%");
        cell.getCellStyle().setDataFormat(iFormat);         
        cell.setCellFormula("1/"+ String.valueOf(iFormat));
        //cell.setCellValue("1");

        //iFormat = dataFormat.getFormat("000000%");
        //first_cell.getCellStyle().setDataFormat(iFormat);         


        FileOutputStream fileOut = new FileOutputStream("workbook.xls");
        wb.write(fileOut);
        fileOut.close();


Also using cell.setCellValue("1") looks like it is not working as it will not 
cause the cell to effectivelly use any of the specified formats (set on the 
cell before or after call to cell.setCellValue()), but that is another story...
Therefore in this example I am using cell.setCellFormula() with index number of 
the format written into the sheet.


If the code

        //HSSFCell first_cell = cell;
         .
         .
         .
        //iFormat = dataFormat.getFormat("000000%");
        //first_cell.getCellStyle().setDataFormat(iFormat);         

you will see format of all the touched cells set to 000000%.
Comment 1 Zdenek Hrib 2006-04-24 12:29:28 UTC
Created attachment 18162 [details]
example source
Comment 2 Zdenek Hrib 2006-04-24 12:30:11 UTC
Created attachment 18163 [details]
compile and run batch file
Comment 3 Zdenek Hrib 2006-04-24 12:31:49 UTC
Created attachment 18164 [details]
example result workbook file
Comment 4 Yegor Kozlov 2009-04-04 06:19:13 UTC
You are applying data format to the same cell style, that is why it affects all the cells. To apply per-cell format you need to create a style for each cell:

        style = wb.createCellStyle();
        style.setDataFormat(iFormat);
        cell.setCellFormula("1/"+ String.valueOf(iFormat));
        cell.setCellStyle(style);

Yegor