POI Font and font style

Source: Internet
Author: User

This chapter describes how to set different fonts, apply styles, and display text in an Excel spreadsheet in different angles of direction.

Each system comes with a large font such as Arial, Impact, Times New Roman, and other font collections that can also be updated with new fonts, if needed. There are also various styles in which the fonts can be displayed, for example, bold, italic, underline, strikethrough, etc. font and font styles

The following code is used for specific fonts and styles applied to the contents of a cell.

Import Java.io.File;
Import Java.io.FileOutputStream;
Import Org.apache.poi.hssf.util.HSSFColor;
Import Org.apache.poi.xssf.usermodel.XSSFCell;
Import Org.apache.poi.xssf.usermodel.XSSFCellStyle;
Import Org.apache.poi.xssf.usermodel.XSSFFont;
Import Org.apache.poi.xssf.usermodel.XSSFRow;
Import Org.apache.poi.xssf.usermodel.XSSFSheet;
Import Org.apache.poi.xssf.usermodel.XSSFWorkbook; public class FontStyle {public static void main (string[] args) throws Exception {Xssfworkbook workbook = NE 
      W Xssfworkbook ();
      XSSFSHEET spreadsheet = Workbook.createsheet ("FontStyle");
      Xssfrow row = Spreadsheet.createrow (2);
      Create a new font and alter it.
      Xssffont font = Workbook.createfont ();
      Font.setfontheightinpoints ((short) 30);
      Font.setfontname ("IMPACT");
      Font.setitalic (TRUE);
      Font.setcolor (HSSFColor.BRIGHT_GREEN.index);
      Set font into style xssfcellstyle style = Workbook.createcellstyle ();
     Style.setfont (font); Create a cell with a value and set style to it.
      Xssfcell cell = Row.createcell (1);
      Cell.setcellvalue ("Font Style");
      Cell.setcellstyle (style);
      FileOutputStream out = new FileOutputStream (New File ("fontstyle.xlsx"));
      Workbook.write (out);
      Out.close ();
   System.out.println ("fontstyle.xlsx written successfully");
 }
}

Let's save the above code in a file named Fontstyle.java. Compile and execute it from the command prompt as follows.

$javac Fontstyle.java
$java fontstyle

It generates an Excel file named Fontstyle.xlsx in the current directory and displays the following output at the command prompt.

Fontstyle.xlsx written successfully

The fontstyle.xlsx file is shown below. Text Direction

Here, you can learn how to set the direction of text in different angles. The contents of the cell are usually displayed horizontally, left to right, and at 00 corners, but you can use the following code to rotate the direction of the text, if necessary.

Import Java.io.File;
Import Java.io.FileOutputStream;
Import Org.apache.poi.xssf.usermodel.XSSFCell;
Import Org.apache.poi.xssf.usermodel.XSSFCellStyle;
Import Org.apache.poi.xssf.usermodel.XSSFRow;
Import Org.apache.poi.xssf.usermodel.XSSFSheet;
Import Org.apache.poi.xssf.usermodel.XSSFWorkbook; public class TextDirection {public static void main (string[] args) throws Exception {Xssfworkbook workbook 
      = new Xssfworkbook ();
      XSSFSHEET spreadsheet = Workbook.createsheet ("Text direction");
      Xssfrow row = Spreadsheet.createrow (2);
      Xssfcellstyle MyStyle = Workbook.createcellstyle ();
      Mystyle.setrotation ((short) 0);
      Xssfcell cell = Row.createcell (1);
      Cell.setcellvalue ("0D angle");
      Cell.setcellstyle (MyStyle);
      Degrees Mystyle=workbook.createcellstyle ();
      Mystyle.setrotation ((short) 30);
      Cell = Row.createcell (3);
      Cell.setcellvalue ("30D angle");
      Cell.setcellstyle (MyStyle);
   Degrees   Mystyle=workbook.createcellstyle ();
      Mystyle.setrotation ((short) 90);
      Cell = Row.createcell (5);
      Cell.setcellvalue ("90D angle");
      Cell.setcellstyle (MyStyle);
      Degrees Mystyle=workbook.createcellstyle ();
      Mystyle.setrotation ((short) 120);
      Cell = Row.createcell (7);
      Cell.setcellvalue ("120D angle");
      Cell.setcellstyle (MyStyle);
      Degrees MyStyle = Workbook.createcellstyle ();
      Mystyle.setrotation ((short) 270);
      Cell = Row.createcell (9);
      Cell.setcellvalue ("270D angle");
      Cell.setcellstyle (MyStyle);
      Degrees Mystyle=workbook.createcellstyle ();
      Mystyle.setrotation ((short) 360);
      Cell = Row.createcell (12);
      Cell.setcellvalue ("360D angle");
      Cell.setcellstyle (MyStyle);
      FileOutputStream out = new FileOutputStream (New File ("textdirection.xlsx"));
      Workbook.write (out);
      Out.close (); System.out.println ("Textdirection.xlsx WrittEn successfully ");
 }
}

Keep the code above the Textdirectin.java file, and then compile and execute it from the command prompt as follows.

$javac Textdirection.java
$java textdirection

This compiles and executes to generate an Excel file named Textdirection.xlsx in the current directory and displays the following output at the command prompt.

Textdirection.xlsx written successfully

The textdirection.xlsx file is shown below.

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.