Get workbook Hssfworkbook hwb = new Hssfworkbook ();//Get Sheethssfsheet sheet = hwb.createsheet ();//Get Style object Hssfcellstyle CellStyle = Hwb.createcellstyle ();//sets the Style object, where only the Border property Cellstyle.setborderbottom (Hssfcellstyle.border_thin) is set; Bottom Border Cellstyle.setborderleft (Hssfcellstyle.border_thin);//Left Border Cellstyle.setbordertop (Hssfcellstyle.border_thin) ;//Top Border Cellstyle.setborderright (Hssfcellstyle.border_thin);//Right Border//to a cell set the bounding box Row0 = Sheet.createrow (i); cell0 = Row0.createcell (j); Cell0.setcellstyle (Style1); Cell0.setcellstyle ("Test");
This is the specified cell set style (border), cannot get the cell style is set, so that all the cells on the page are set to a border, very ugly ...
The following transfers are 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);
Java POI Settings Border