POI set Excel cell format to text, decimal, percent, currency, date, scientific notation, and Chinese capitalization

Source: Internet
Author: User

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

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.