2012-10-09 27 views
0

从java类中,我将值54666700导出为.xlsx文档。我想将其显示为54,666,700美元,但在文档中应该将其存储为54666700,即,当我选择该列时,应将数值选为数字54666700.Java - 格式化数字并导出为ex​​cel doc

就格式设置为货币而言,我使用下面的码。

Long newsValue = 54666700 
NumberFormat testFormat = NumberFormat.getCurrencyInstance(Locale.US); 
testFormat.setMinimumFractionDigits(0); 
String currency = testFormat.format(current); 
//which will be $54,666,700 

我发现这个库Apache DataFormatter,但无法弄清楚如何获得Cell对象那里。

任何意见/技巧赞赏。

+0

哪个区域是你工作? – TheBlastOne

+0

您的意思是语言环境国家类型.. .. Locale.US –

回答

2

你可以使用HSSFDataFormat(POI 3.0),下面是使用例子:

try{ 
    FileOutputStream out = new FileOutputStream 
("dateFormat.xls"); 
    HSSFWorkbook hssfworkbook = new HSSFWorkbook(); 
    HSSFSheet sheet = hssfworkbook.createSheet 
("new sheet"); 
    HSSFCellStyle cs = hssfworkbook.createCellStyle(); 
    HSSFDataFormat df = hssfworkbook. 
createDataFormat(); 
    cs.setDataFormat(df.getFormat("#,##0.0")); 
    HSSFRow row = sheet.createRow((short)0); 
    HSSFCell cell = row.createCell((short)0); 
    cell.setCellValue(11111.1); 
    cell.setCellStyle(cs); 
    hssfworkbook.write(out); 
    out.close(); 
}catch(Exception e){} 
} 
+0

在工作簿中,值将是$ 11,111,但如果我们在功能栏中看到它将是1111.1? –