Write number in excel cells using Apache POI

后端 未结 1 865
北荒
北荒 2020-12-10 01:45

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?

相关标签:
1条回答
  • 2020-12-10 02:03

    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

    0 讨论(0)
提交回复
热议问题