Before reading this article, please read my previous article, the previous article has focused on POI set Excel cell format as text format, the rest of the set decimal, percent, currency, date, scientific notation and Chinese capitalization these will be written in one by one below
Each of the following will be used in these three rows of variables
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 year M D Day"));
Cell.setcellstyle (CellStyle);
Second: Preserve two-bit 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, the reason for this is because 0.00 is an Excel embedded format, the complete list of Excel inline format you can see the custom list in this window:
It's not listed 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);
Fourth type: 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.
Fifth type: Chinese capitalization format
Cell.setcellvalue (20000);
Hssfcellstyle CellStyle = Demoworkbook.createcellstyle ();
Hssfdataformat format= Demoworkbook.createdataformat ();
Cellstyle.setdataformat (Format.getformat ("[dbnum2][$-804]0"));
Cell.setcellstyle (CellStyle);
Type sixth: Scientific notation 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
POI set Excel cell format to text, decimal, percent, currency, date, scientific notation, and Chinese capitalization