Reprinted from: http://z3sm2012.iteye.com/blog/1446669
Some summary of operations that need to format Excel cells may be used in POI:
Get the Workbook object first:
Hssfworkbook wb = new Hssfworkbook ();
Hssfsheet sheet = Wb.createsheet ();
Hssfcellstyle SetBorder = Wb.createcellstyle ();
First, set the background color:
Setborder.setfillforegroundcolor ((short) 13);//Set Background color
Setborder.setfillpattern (Hssfcellstyle.solid_foreground);
Second, set the border:
Setborder.setborderbottom (Hssfcellstyle.border_thin); Bottom Frame
Setborder.setborderleft (Hssfcellstyle.border_thin);//left Border
Setborder.setbordertop (Hssfcellstyle.border_thin);//Top Border
Setborder.setborderright (Hssfcellstyle.border_thin);//Right Border
Third, set the center:
Setborder.setalignment (Hssfcellstyle.align_center); Center
Four, set the font:
Hssffont font = Wb.createfont ();
Font.setfontname ("Blackbody");
Font.setfontheightinpoints ((short) 16);//Set Font size
Hssffont Font2 = Wb.createfont ();
Font2.setfontname ("Imitation _gb2312");
Font2.setboldweight (hssffont.boldweight_bold);//Bold display
Font2.setfontheightinpoints ((short) 12);
Setborder.setfont (font);//Select the font format you want to use
Five, set the column width:
Sheet.setcolumnwidth (0, 3766); The first parameter represents the column ID (starting at 0), and the 2nd parameter represents the Width value
Six, set the automatic line wrapping:
Setborder.setwraptext (TRUE);//Set line wrapping
Seven, merge the cells:
Region Region1 = new Region (0, (short) 0, 0, (short) 6);
Parameter 1: line number parameter 2: Starting column number parameter 3: line number parameter 4: terminating column number
or with
Cellrangeaddress Region1 = new Cellrangeaddress (RowNumber, RowNumber, (short) 0, (short) 11);
However, it should be noted that the parameters of the two constructor methods are not the same, depending on the different versions of the POI.
Sheet.addmergedregion (Region1);
So much has been used so far, and new additions will continue to follow.
Reprint: Poi set Excel table border, font, etc.