Set the Excel cell format in poi

Source: Internet
Author: User

Reference: http://apps.hi.baidu.com/share/detail/17249059

Poi may use some Operation Summaries for setting the Excel cell format:

Obtain the thin objects first:

Hssfworkbook WB = new hssfworkbook ();

Hssfsheet sheet = WB. createsheet ();

Hssfcellstyle setborder = WB. createcellstyle ();

1. Set the background color:

Setborder. setfillforegroundcolor (short) 13); // you can specify the background color.
Setborder. setfillpattern (hssfcellstyle. solid_foreground );

2. Set the border:

Setborder. setborderbottom (hssfcellstyle. border_thin); // Bottom Border
Setborder. setborderleft (hssfcellstyle. border_thin); // left border
Setborder. setbordertop (hssfcellstyle. border_thin); // The upper border
Setborder. setborderright (hssfcellstyle. border_thin); // right border

3. Set center:

Setborder. setalignment (hssfcellstyle. align_center); // center

4. Set the font:

Hssffont font = WB. createfont ();
Font. setfontname (" ");
Font. setfontheightinpoints (short) 16); // you can specify the font size.

Hssffont font2 = WB. createfont ();
Font2.setfontname (" _ gb2312 ");
Font2.setboldweight (hssffont. boldweight_bold); // bold display
Font2.setfontheightinpoints (short) 12 );

Setborder. setfont (font); // select the font format

5. Set the column width:

Sheet. setcolumnwidth (0, 3766); // The first parameter indicates the column ID (starting from 0). The first parameter indicates the width value. For details, refer to "2012-08-10". The width is 2nd.

6. Set automatic line feed:

Setborder. setwraptext (true); // set automatic line feed

7. Merge cells:

Region region1 = new region (0, (short) 0, 0, (short) 6 );

// Parameter 1: row number parameter 2: Start column Number Parameter 3: row number parameter 4: End column number

Or use

Cellrangeaddress region1 = new cellrangeaddress (rownumber, rownumber, (short) 0, (short) 11 );

However, you should note that the parameters of the two constructor methods are not the same. The specific version of POI depends on which one is used.
Sheet. addmergedregion (region1 );

There are so many currently used products that will be added later.

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.