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%.
Created attachment 18162 [details] example source
Created attachment 18163 [details] compile and run batch file
Created attachment 18164 [details] example result workbook file
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