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

Rav*_*mar 23 java excel apache-poi

如何在poi的帮助下在单元格中写出我的全部价值?

即如果值是1000.000,那么如何在不"#"之后截断000的情况下写出这个完整的值.与POI?意味着我想要全部价值.

在我的情况下,它只需要1000,但这对我来说不是正确的格式.

Sha*_*rma 34

您必须设置存储此数字的单元格的"格式".示例代码:

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();
Run Code Online (Sandbox Code Playgroud)

资料来源:http://poi.apache.org/spreadsheet/quick-guide.html#DataFormats