以下代码是用于创建具有文本值的excel单元格的apache poi代码,该文本值需要文本值作为货币。
代码:
row1= sheetNormal.createRow((short)2);
row1.setHeight((short)(256 *1.5));
header = row1.createCell((short)1);
header.setCellValue(Integer.parseInt(500000));
Excel中的期望输出:-5,00,000
输出以文本形式显示。
要获得货币输出,您可以执行以下操作。请注意,methodsetStringValue(value)
接受的值为a double
,而不是Integer。您可以从分析它String
以Double
比操作,这取决于你用它来输入你的价值观精度此值。此精度可以是:
在下面的代码中,您可以找到所有三个示例(header
由更改cell1
):
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("test sheet");
HSSFCellStyle currencyStyle = wb.createCellStyle();
HSSFDataFormat dataFormat = wb.createDataFormat();
currencyStyle.setDataFormat(dataFormat.getFormat("$#,##0.00.000"));
HSSFRow row1 = sheet.createRow(2);
row1.setHeight((short) (256*1.5));
// Input in DOLLARS
HSSFCell cell1 = row1.createCell(1);
cell1.setCellStyle(currencyStyle);
cell1.setCellValue(Double.parseDouble("5")); // output $5,00,000
// Input in CENTS
HSSFCell cell2 = row1.createCell(2);
cell2.setCellStyle(currencyStyle);
cell2.setCellValue(Double.parseDouble("499") / 100); // output $4,99,000
// Input in 1/1000 OF A CENT
HSSFCell cell3 = row1.createCell(3);
cell3.setCellStyle(currencyStyle);
cell3.setCellValue(Double.parseDouble("123456789") / 100000); // output $1 234,56,789
try {
FileOutputStream out = new FileOutputStream("currency_data_format.xls");
wb.write(out);
out.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
如果在单元格中看到########,则是因为您的值包含太多字符。增加像元宽度。
如果您对输入数据的方式给出一些精确的说明,则可以修改上面的代码。祝好运!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句