Write number in excel cells using Apache POI

Ravi Parmar picture Ravi Parmar · Mar 17, 2011 · Viewed 61k times · Source

How can I write my full value in cell with help of poi ?

i.e. if value is 1000.000 then how can I write this full value without truncating 000 after "." with POI? means I want full value.

In my case, it only takes 1000 but this is not right format for me.

Answer

Shamit Verma picture Shamit Verma · Mar 17, 2011

You have to set "Format" of the cell where this number is getting stored. Example code:

Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("format sheet");
CellStyle style;
DataFormat format = wb.createDataFormat();
Row row;
Cell cell;
short rowNum = 0;
short colNum = 0;

row = sheet.createRow(rowNum++);
cell = row.createCell(colNum);
cell.setCellValue(11111.25);
style = wb.createCellStyle();
style.setDataFormat(format.getFormat("0.0"));
cell.setCellStyle(style);

row = sheet.createRow(rowNum++);
cell = row.createCell(colNum);
cell.setCellValue(11111.25);
style = wb.createCellStyle();
style.setDataFormat(format.getFormat("#,##0.0000"));
cell.setCellStyle(style);

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

Source : http://poi.apache.org/spreadsheet/quick-guide.html#DataFormats