POI Format cell style

Source: Internet
Author: User

An example shows how to set the border, font, color, size, underline, merge, and alignment of Excel cells through a POI.

The Excel file is as follows:

Java code    package my.excel;     import Java.io.FileOutputStream;     import Org.apache.poi.ss.usermodel.Cell;   import Org.apache.poi.ss.usermodel.CellStyle;   import Org.apache.poi.ss.usermodel.Font;   import Org.apache.poi.ss.usermodel.Row;   import Org.apache.poi.ss.usermodel.Sheet;   import Org.apache.poi.ss.usermodel.Workbook;   import org.apache.poi.ss.util.CellRangeAddress;   import Org.apache.poi.xssf.usermodel.XSSFCellStyle;   import Org.apache.poi.xssf.usermodel.XSSFColor;   import Org.apache.poi.xssf.usermodel.XSSFWorkbook;      public class Cellformatexcel {              public static void Main (string[] args) {           try {           &NBsp;  //Create an Excel table workbook                workbook wb = new Xssfworkbook ();               sheet Sheet = Wb.createSheet ( "Table cell formatting");                               //============================                //       Set the font               //================ for a cell ============               row ZtRow = Sheet.createrow ((short) 0);               cell ZtCell = Ztrow.createcell (0);      &Nbsp;        ztcell.setcellvalue ("China");               //creating a cell style object                xssfcellstyle ZtStyle = (XSSFCellStyle) Wb.createcellstyle ();               //Creating Font Objects                font Ztfont = Wb.createFont ();               ztfont.setitalic (TRUE);                      //set Font to italic                Ztfont.setcolor (font.color_red);            //set the font to "red"      &Nbsp;        ztfont.setfontheightinpoints ((short) 22);    //set the font size to 18px                ztfont.setfontname ("XXFarEastFont-Xingkai");             //apply a "Chinese Xingkai" font to the current cell                ztfont.setunderline (Font.U_DOUBLE);         //Add (font.u_single single underline/font.u_double double underline)   //          ztfont.setstrikeout (TRUE);                  // Whether to add strikethrough               ztstyle.setfont ( Ztfont);                     //apply fonts to styles above  &Nbsp;            ztcell.setcellstyle (ZtStyle); The               //style is applied to the cell                                //============================                //        Set cell borders               //================= ===========               row BorderRow = Sheet.createrow (2);               cell BorderCell = Borderrow.createcell (1);               bordercEll.setcellvalue ("China");               //creating a cell style object                xssfcellstyle BorderStyle = ( Xssfcellstyle) Wb.createcellstyle ();               //set cell border style                //cellstyle.border_double        Bilateral line               // Cellstyle.border_thin         Fine Edges                //Cellstyle.border_medium       Medium Edge                //CellStyle.BORDER_DASHED        Dashed Edge                //Cellstyle.border_hair         Small dot dashed edge               //CellStyle.BORDER_ THICK        Coarse edges                borderstyle.setborderbottom (Cellstyle.border_thick);               borderstyle.setbordertop ( cellstyle.border_dashed);               borderstyle.setborderleft ( cellstyle.border_double);               borderstyle.setborderright (Cellstyle.border_thin);                               //set cell border color &Nbsp;              Borderstyle.setbottombordercolor (New Xssfcolor (Java.awt.Color.RED));                Borderstyle.settopbordercolor (New Xssfcolor (Java.awt.Color.GREEN));                Borderstyle.setleftbordercolor (New Xssfcolor (Java.awt.Color.BLUE));                               bordercell.setcellstyle (BorderStyle);                               //============================                //      To set the alignment of a cell's contents               //======================== = = =               row AlignRow = Sheet.createrow (4);               cell AlignCell = Alignrow.createcell (1);               aligncell.setcellvalue ("China ");                               //creating a cell style object                xssfcellstyle Alignstyle = (XSSFCellStyle) Wb.createcellstyle ();                               //set the cell content horizontally to its way              //XSSFCellStyle.ALIGN_ Center        Center Alignment                //Xssfcellstyle.align_left          left align                //XSSFCellStyle.ALIGN_RIGHT         Right Alignment                alignstyle.setalignment (Xssfcellstyle.align_center);                               //set the contents of a cell vertically on its way                //Xssfcellstyle.vertical_top     Align       &nbs on    p;       //xssfcellstyle.vertical_center     Alignment                //Xssfcellstyle.vertical_bottom      under Alignment                Alignstyle.setverticalalignment (Xssfcellstyle.vertical_center);                               aligncell.setcellstyle (AlignStyle);                               //============================                //      Sets the height and width of a cell               //============================                row Sizerow = Sheet.createrow (6);               sizerow.setheightinpoints ( 30);                  //Setting the height of a row                               cell Sizecell = SizeRow.createCell (1);                 string Sizecellvalue = "Java programming thought"; The length of the            //string is 10, which indicates that there are 10 characters in the string, ignoring Chinese    english             sizecell.setcellvalue (SizeCellValue);                   //sets the length of the cell to Sizecellvlue. And the number of bytes Sheet.setcolumnwidth uses Sizecellvlue                //sizecellvalue.getbytes (). length = =                sheet.setcolumnwidth (1, (Sizecellvalue.getbytes (). Length) * 256);                               //============================                //      Set cell wrap               //=============== =============               row WrapRow = Sheet.createrow (8);     &nBsp;         cell Wrapcell = Wraprow.createcell (2);               wrapcell.setcellvalue (" Bao Jianfeng from sharpening out, plum blossom fragrance from bitter cold Come ");                               //creating a cell style object                xssfcellstyle Wrapstyle = (XSSFCellStyle) wb.createCellStyle ();               wrapstyle.setwraptext (True );                     //Set whether cell contents wrap                Wrapcell.setcellstyle (Wrapstyle);                              //=============== =============               //          Merge Unit Columns                //============================                row Regionrow = Sheet.createrow (12);               cell RegionCell = Regionrow.createcell (0);               regioncell.setcellvalue (" Bao Jianfeng from sharpening out, plum blossom fragrance from bitter cold Come ");                               //A, B, c three columns in line 13th          &nBsp;     cellrangeaddress region = new Cellrangeaddress (12, 12, 0, 2); The parameters are all starting from O                Sheet.addmergedregion (region);                               //============================                //          Merging units cells and columns               //==== ========================                Row RegionRow2 = Sheet.createrow (13);               cell regionCell2 = Regionrow2.createcell (3);               string region2value = "Bao Jianfeng from sharpening out, plum blossom from bitter cold." "                                  + "Harvest flowers into honey, for who hard for who sweet." "                                  + "The song and then Xiao Sound, view thousands of swords and then the device." "                                  + "Can know the people, and this is the ancient. ";               regioncell2.setcellvalue ( Region2value);                           &nbsp   //A, B, c three columns in line 13th                cellrangeaddress Region2 = new Cellrangeaddress (13, 17, 3, 7); The parameters are all starting from O                Sheet.addmergedregion (Region2);                               xssfcellstyle Region2Style = (XSSFCellStyle) Wb.createcellstyle ();                Region2style.setverticalalignment (Cellstyle.vertical_center);               region2style.setwraptext ( true);                      //Set whether cell contents wrap               regioncell2.setcellstyle (Region2style);                                            //============================                //writing an Excel file to disk                //============================                fileoutputstream is = new FileOutputStream ("document/cellformatexcel.xlsx");               wb.write (IS);               is.close ();                               System.out.println ("Write successful, run end!") ");           } catch (Exception e) {               e.printstacktrace ();           }           } 

Poi Format cell style

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.