Set Data Format in Excel Using POI 3.0(XSSF/HSSF)

http://www.roseindia.net/java/poi/setdataformat.shtml

In this program we are setting data format in excel file using Java. 

POI version 3.0 provides a new feature for manipulating various file formats based upon Microsoft's OLE 2 Compound Document format using Java. POI version 3.0 APIs provides user defined formatting facility and also provides a list of build-in data format (for String, Integer etc. values).

The org.apache.poi.hssf.usermodel.HSSFDataFormat class extends java.lang.Object class. This class provides utility to identify built-in formats. There is facility in POI 3.0 to define the user define formats.
 
 The following is a list of built-in formats provided is POI 3.0:

S/No.

Format 

VlaueS/No

Format

Value 
1General012h:mm AM/PM0x12
20113h:mm:ss AM/PM0x13
30.00214h:mm0x14
4#,##0315h:mm:ss0x15
5#,##0.00416m/d/yy h:mm0x16
6($#,##0_);($#,##0)517(#,##0_);[Red](#,##0)0x26
7($#,##0_);[Red]($#,##0)618(#,##0.00_);(#,##0.00)0x27
8($#,##0.00);($#,##0.00)719(#,##0.00_);[Red](#,##0.00)0x28
9($#,##0.00_);[Red]($#,##0.00)820_(*#,##0_);_(*(#,##0);_(* \"-\"_);_(@_)0x29
100%921_($*#,##0_);_($*(#,##0);_($* \"-\"_);_(@_)0x2a
110.00%0xa22_(*#,##0.00_);_(*(#,##0.00);_(*\"-\"??_);_(@_)0x2b
120.00E+000xb23_($*#,##0.00_);_($*(#,##0.00);_($*\"-\"??_);_(@_)0x2c
13# ?/?0xc24mm:ss0x2d
14# ??/??0xd25[h]:mm:ss0x2e
15m/d/yy0xe26mm:ss.00x2f
16d-mmm-yy0xf27##0.0E+00x30
17d-mmm0x1028@-This is text format.0x31
18mmm-yy0x1129text-Alias for "@"0x31

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

In this example we are going to set the format as 
"#,##0.0".We are passing value in cell and the output of this will be 11,111.1 .To set the data format we are using setDataFormat() method .In this method we are creating the object of data format. After that we are setting is into hssf cell style sheet.

 The code of the program is given below:

 

import java.io.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.usermodel.HSSFRow;
class setDataFormat
  {
public static void main(String s[])
  {
  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){}
 }
}

The output of the program is given below:

Download this example.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值