简体   繁体   English

使用Apache POI在Excel单元格中写入数字

[英]Write number in excel cells using Apache POI

How can I write my full value in cell with help of poi ? 如何在poi的帮助下在单元格中写出我的全部价值?

ie if value is 1000.000 then how can I write this full value without truncating 000 after "." 即如果值是1000.000,那么如何在不删除“000”之后的000的情况下写出这个完整的值。 with POI? 与POI? means I want full value. 意味着我想要全部价值。

In my case, it only takes 1000 but this is not right format for me. 在我的情况下,它只需要1000,但这对我来说不是正确的格式。

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 资料来源: http//poi.apache.org/spreadsheet/quick-guide.html#DataFormats

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM