gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 20:34:11 27 4
gpt4 key购买 nike

如何在 poi 的帮助下将我的全部值(value)写在单元格中?

即如果值为 1000.000,那么如何在不截断“。”之后的 000 的情况下写入这个完整值。与兴趣点?意味着我想要完整的值(value)。

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

最佳答案

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

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();

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

关于java - 使用 Apache POI 在 Excel 单元格中写入数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5335285/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com