[转]Hssfworkbook 设置单元格格式 setdataformat

HSSFWorkbook demoWorkBook = new HSSFWorkbook();   

 HSSFSheet demoSheet = demoWorkBook.createSheet("The World's 500 Enterprises");   

 HSSFCell cell = demoSheet.createRow(0).createCell(0);

1.设置单元格为文本格式

 HSSFCellStyle cellStyle2 = demoWorkBook.createCellStyle();

 HSSFDataFormat format = demoWorkBook.createDataFormat();

 cellStyle2.setDataFormat(format.getFormat("@"));

 cell.setCellStyle(cellStyle2);


第一种:日期格式

            cell.setCellValue(new Date(2008,5,5));

            //set date format

            HSSFCellStyle cellStyle = demoWorkBook.createCellStyle();

            HSSFDataFormat format= demoWorkBook.createDataFormat();

            cellStyle.setDataFormat(format.getFormat("yyyy年m月d日"));

            cell.setCellStyle(cellStyle);


第二种:保留两位小数格式

            cell.setCellValue(1.2);

            HSSFCellStyle cellStyle = demoWorkBook.createCellStyle();

            cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00"));

            cell.setCellStyle(cellStyle);


第三种:货币格式

            cell.setCellValue(20000);

            HSSFCellStyle cellStyle = demoWorkBook.createCellStyle();

            HSSFDataFormat format= demoWorkBook.createDataFormat();

            cellStyle.setDataFormat(format.getFormat("¥#,##0"));

            cell.setCellStyle(cellStyle);

第四种:百分比格式

            cell.setCellValue(20);

            HSSFCellStyle cellStyle = demoWorkBook.createCellStyle();

            cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00%"));

            cell.setCellStyle(cellStyle);

  此种情况跟第二种一样

第五种:中文大写格式

            cell.setCellValue(20000);

            HSSFCellStyle cellStyle = demoWorkBook.createCellStyle();

            HSSFDataFormat format= demoWorkBook.createDataFormat();

            cellStyle.setDataFormat(format.getFormat("[DbNum2][$-804]0"));

            cell.setCellStyle(cellStyle);

第六种:科学计数法格式

            cell.setCellValue(20000);

            HSSFCellStyle cellStyle = demoWorkBook.createCellStyle();

            cellStyle.setDataFormat( HSSFDataFormat.getBuiltinFormat("0.00E+00"));

            cell.setCellStyle(cellStyle);

The following is a list of built-in formats provided is POI 3.0:

S/No.

Format 

Vlaue S/No

Format

Value 
1 General 0 12 h:mm AM/PM 0x12
2 0 1 13 h:mm:ss AM/PM 0x13
3 0.00 2 14 h:mm 0x14
4 #,##0 3 15 h:mm:ss 0x15
5 #,##0.00 4 16 m/d/yy h:mm 0x16
6 ($#,##0_);($#,##0) 5 17 (#,##0_);[Red](#,##0) 0x26
7 ($#,##0_);[Red]($#,##0) 6 18 (#,##0.00_);(#,##0.00) 0x27
8 ($#,##0.00);($#,##0.00) 7 19 (#,##0.00_);[Red](#,##0.00) 0x28
9 ($#,##0.00_);[Red]($#,##0.00) 8 20 _(*#,##0_);_(*(#,##0);_(* \"-\"_);_(@_) 0x29
10 0% 9 21 _($*#,##0_);_($*(#,##0);_($* \"-\"_);_(@_) 0x2a
11 0.00% 0xa 22 _(*#,##0.00_);_(*(#,##0.00);_(*\"-\"??_);_(@_) 0x2b
12 0.00E+00 0xb 23 _($*#,##0.00_);_($*(#,##0.00);_($*\"-\"??_);_(@_) 0x2c
13 # ?/? 0xc 24 mm:ss 0x2d
14 # ??/?? 0xd 25 [h]:mm:ss 0x2e
15 m/d/yy 0xe 26 mm:ss.0 0x2f
16 d-mmm-yy 0xf 27 ##0.0E+0 0x30
17 d-mmm 0x10 28 @-This is text format. 0x31
18 mmm-yy 0x11 29 text-Alias for "@" 0x31

Note:-The values from 0x17 to 0x24 are reserved for international and undocumented and 0x25 is for "(#,##0_);(#,##0)".

转自:http://blog.sina.com.cn/s/blog_6793ffde0101l121.html

你可能感兴趣的:(Excel,单元格格式,POI,POI,excel)