Total Pageviews

2013/11/14

How to set currecny cell to right-justified horizontal alignment and apply 1000 separator in Apache POI

Problem
We utilized Apache POI to write excel, but some currency cells do not right-justified horizontal alignment and do not apply 1000 separator.

Solution
1:          //create CellStyle, and define style information  
2:          CellStyle cs = workbook.createCellStyle();  
3:          cs.setBorderBottom((short) 1);  
4:          cs.setBorderTop((short) 1);  
5:          cs.setBorderLeft((short) 1);  
6:          cs.setBorderRight((short) 1);  
7:          cs.setAlignment(CellStyle.ALIGN_RIGHT);//right-justified horizontal alignment  
8:          cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("#,##0.00"));//apply 1000 separator  
9:          .....  
10:          .....  
11:          //set cs, the CellStyle with style information which we defined, into cell  
12:          cell.setCellStyle(cs);  


For more DataFormat information, you can check it:

Check the result

Here you can find more useful information: http://javacrazyer.iteye.com/blog/894850

No comments: