Hssfcellstyle Cellpercentstyle = Workbook.createcellstyle ();
Cellpercentstyle.setdataformat ((short) 10);//percent
One, the first way
POI custom preserves a decimal format
Hssfcellstyle Celldigitstyle = Workbook.createcellstyle ();
Celldigitstyle.setdataformat (Workbook.createdataformat (). GetFormat ("0.0"));
can also be customized into other formats, 5 formatted into 05
Hssfcellstyle Celldigitstyle = Workbook.createcellstyle ();
Celldigitstyle.setdataformat (Workbook.createdataformat (). GetFormat ("00"));
Second, two ways
Hssfcellstyle cellDigitStyle2 = Workbook.createcellstyle ();
Celldigitstyle2.setdataformat (Hssfdataformat.getbuiltinformat ("0.00"));//Two decimal
Celldigitstyle2.setdataformat ((short) 2); Function ditto
Note: The second is not a custom, if the Getbuiltinformat return parameters, formatting will be invalidated.
For example, Hssfdataformat.getbuiltinformat ("00") returns only 1, so that formatting is invalidated.
So to realize excel-> Format cell-> Custom, only know the first way.
The variables in these three rows will be used for each of the following
Hssfworkbook Demoworkbook = new Hssfworkbook ();
Hssfsheet Demosheet = Demoworkbook.createsheet ("The World ' s Enterprises");
Hssfcell cell = Demosheet.createrow (0). Createcell (0);
First Type: Date format
Cell.setcellvalue (New Date (2008,5,5));
Set Date format
Hssfcellstyle CellStyle = Demoworkbook.createcellstyle ();
Hssfdataformat format= Demoworkbook.createdataformat ();
Cellstyle.setdataformat (Format.getformat ("yyyy m-month D-Day"));
Cell.setcellstyle (CellStyle);
Second: Keep two-digit decimal format
Cell.setcellvalue (1.2);
Hssfcellstyle CellStyle = Demoworkbook.createcellstyle ();
Cellstyle.setdataformat (Hssfdataformat.getbuiltinformat ("0.00"));
Cell.setcellstyle (CellStyle);
This is different from the above, using the Hssfdataformat.getbuiltinformat () method, because 0.00 is in Excel embedded format, the complete list of Excel embedded format you can see the custom list in this window:
There's no listing here.
The third type: Currency format
Cell.setcellvalue (20000);
Hssfcellstyle CellStyle = Demoworkbook.createcellstyle ();
Hssfdataformat format= Demoworkbook.createdataformat ();
Cellstyle.setdataformat (Format.getformat ("¥#,# #0"));
Cell.setcellstyle (CellStyle);
type Fourth: Percent format
Cell.setcellvalue (20);
Hssfcellstyle CellStyle = Demoworkbook.createcellstyle ();
Cellstyle.setdataformat (Hssfdataformat.getbuiltinformat ("0%"));
Cell.setcellstyle (CellStyle);
This situation is the same as the second
The fifth type: Chinese capital format
Cell.setcellvalue (20000);
Hssfcellstyle CellStyle = Demoworkbook.createcellstyle ();
Hssfdataformat format= Demoworkbook.createdataformat ();
Cellstyle.setdataformat (Format.getformat ("[dbnum2][$-804]0"));
Cell.setcellstyle (CellStyle);
The sixth kind: The scientific counting method format
Cell.setcellvalue (20000);
Hssfcellstyle CellStyle = Demoworkbook.createcellstyle ();
Cellstyle.setdataformat (Hssfdataformat.getbuiltinformat ("0.00E+00"));
Cell.setcellstyle (CellStyle);
This situation is also the same as in the second case