The styles involved are described in the code:
1 Packagecom.it.poiTest;2 3 Importjava.io.FileNotFoundException;4 ImportJava.io.FileOutputStream;5 Importjava.io.IOException;6 7 ImportOrg.apache.poi.hssf.util.HSSFColor;8 ImportOrg.apache.poi.sl.usermodel.Sheet;9 ImportOrg.apache.poi.ss.usermodel.Cell;Ten ImportOrg.apache.poi.ss.usermodel.Color; One Importorg.apache.poi.ss.usermodel.IndexedColors; A ImportOrg.apache.poi.ss.usermodel.Row; - Importorg.apache.poi.ss.util.CellRangeAddress; - ImportOrg.apache.poi.xssf.usermodel.XSSFCellStyle; the ImportOrg.apache.poi.xssf.usermodel.XSSFColor; - ImportOrg.apache.poi.xssf.usermodel.XSSFSheet; - ImportOrg.apache.poi.xssf.usermodel.XSSFWorkbook; - + Public classMorestylecell { - Public Static voidMain (string[] args) { +Xssfworkbook Workbook =NewXssfworkbook (); A Try { atFileOutputStream out =NewFileOutputStream ("Morestylewroknook.xlsx"); -Xssfsheet sheet = workbook.createsheet ("stylesheet"); - - /** - * Simple set line height - */ in //First line -Row row0 = Sheet.createrow (0); toRow0.setheight (( Short) (500)); + //6th Column -Cell cell = Row0.createcell (5); theCell.setcellvalue ("height=500"); * $ Panax Notoginseng /** - * Test each location after merging cells the */ + //Merge Cell parameter 1: first row/Parameter 2: Last row/Parameter 3: first column/Parameter 4: Last column [within this range] ASheet.addmergedregion (NewCellrangeaddress (1,3,1,4)); theRow row1 = Sheet.createrow (1); +Cell cell1 = Row1.createcell (0); -Cell1.setcellvalue ("first column of second row"); $Cell cell2 = Row1.createcell (1); $Cell2.setcellvalue ("Second row, second column. Should be a merged cell "); - //now that you've merged the cells, look at where the 1.2 "proof doesn't appear." -Cell Cell3 = Row1.createcell (2); theCell3.setcellvalue ("Second row, third column"); - WuyiCell CELL4 = Row1.createcell (5); theCell4.setcellvalue ("Second row, column fifth, the third column after merging cells"); - Wu /** - * Test CellStyle Settings-cell centering and text wrapping in cells About */ $Row0 = Sheet.createrow (4); -Row0.setheight (( Short) 1000); -Cell1 = Row0.createcell (0); -Cell1.setcellvalue ("Line Fifth height=1000"); A //set the width of a column +Sheet.setcolumnwidth (0, 9000); theXssfcellstyle Style1 =Workbook.createcellstyle (); - //Set Style---Horizontal alignment in cell $ style1.setalignment (xssfcellstyle.align_center); the //Set Style---the alignment of the vertical direction in the cell the style1.setverticalalignment (xssfcellstyle.vertical_top); the //to set a style for a certain cell the Cell1.setcellstyle (style1); - //Append content to cell cells in //Gets the value inside the cell "Getrichstringcellvalue gets the value of the rich text type, except for the decimal type of this method to get the Getnumericcellvalue, All other types can use this method to get ToString and then convert to other data types " theString Cell1value =Cell1.getrichstringcellvalue (). toString (); the //Cell1.getnumericcellvalue (); About //cannot be wrapped automatically after adding \ n. theCell1.setcellvalue (cell1value+ "\ n" + "Horizontal center" + "\ n" + "vertical Top"); the //set style wrap so you can wrap it up automatically theStyle1.setwraptext (true); +Cell1.setcellvalue (cell1value+ "\ n" + "Horizontal center" + "\ n" + "vertical top" + "\ n" + "Wrap Line"); - System.out.println (cell1value); the Bayi /** the * Style Settings--Set the border style and color of border the */ -Row0 = Sheet.createrow (5); -Row0.setheight (( Short) 1000); theCell1 = Row0.createcell (5); theCell1.setcellvalue (6.6); theXssfcellstyle Style2 =Workbook.createcellstyle (); the Style2.setborderbottom (Xssfcellstyle.border_thin); - Style2.setborderleft (xssfcellstyle.border_hair); the style2.setborderright (xssfcellstyle.border_dotted); the style2.setbordertop (xssfcellstyle.border_none); the //color three ways to give Way 1:94 Style2.setbottombordercolor (IndexedColors.BLUE.getIndex ()); the //Mode 2 theXssfcolor color =NewXssfcolor (); the byte[] A = {127,0,13};98 //Color.setrgb (a); AboutColor.setargbhex ("FF2906"); - style2.setleftbordercolor (color);101 //Mode 3102 Style2.setrightbordercolor (HSSFColor.BLACK.index);103 Cell1.setcellstyle (style2);104 the /**106 * Style Setting---Setting the cell's background color and fill effect107 */108Row0 = Sheet.createrow (6);109Row0.setheight (( Short) 1200); theCell1 = Row0.createcell (6);111Cell1.setcellvalue (7.7); theXssfcellstyle Style3 =Workbook.createcellstyle ();113 Style3.setfillbackgroundcolor (HSSFColor.RED.index); the //set the fill effect for a cell the Style3.setfillpattern (xssfcellstyle.least_dots); the Cell1.setcellstyle (style3);117 118 119 /** - * Style Settings--Set the front fill color of the cell121 */122Row0 = Sheet.createrow (7);123Row0.setheight (( Short) 1200);124Cell1 = Row0.createcell (7); theCell1.setcellvalue (8.8);126Xssfcellstyle style4 =Workbook.createcellstyle ();127 Style4.setfillforegroundcolor (IndexedColors.GREEN.index); - Style4.setfillpattern (Xssfcellstyle.align_fill);129 Cell1.setcellstyle (style4); the 131 the Workbook.write (out);133 134}Catch(FileNotFoundException e) {135 e.printstacktrace ();136}Catch(IOException e) {137 e.printstacktrace ();138 }139 } $}
View Code
Legend of the last style:
Poi xlsx uses POI to set the cell style of xlsx