2013-01-21 23:52 POI Cell formatting-data formatting

Source: Internet
Author: User

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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.