Apache Poi 简明教程

Apache POI - Fonts

本章介绍如何在 Excel 电子表格中设置不同的字体、应用样式和在不同的角度方向显示文本。

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

每个系统都捆绑了大量的字体,如 Arial、Impact、Times New Roman 等。如果需要,还可以使用新字体更新此集合。类似地,可以显示字体的各种样式,例如粗体、斜体、下划线、删除线等。

Every system comes bundled with a huge collection of fonts such as Arial, Impact, Times New Roman, etc. The collection can also be updated with new fonts, if required. Similarly there are various styles in which a font can be displayed, for example, bold, italic, underline, strike through, etc.

Fonts and Font Styles

以下代码用于将特定的字体和样式应用于单元格的内容。

The following code is used to apply a particular font and style to the contents of a cell.

import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.hssf.util.IndexedColors;
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 = new 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(IndexedColors.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");
   }
}

让我们将上述代码保存在名为 FontStyle.java 的文件中。从命令提示符编译并执行它,如下所示−

Let us 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

它会在您的当前目录中生成一个名为 fontstyle.xlsx 的 Excel 文件,并显示命令提示符上的以下输出。

It generates an Excel file named fontstyle.xlsx in your current directory and display the following output on the command prompt.

fontstyle.xlsx written successfully

fontstyle.xlsx 文件如下所示−

The fontstyle.xlsx file looks as follows −

fontstyle

Text Direction

在这里,您可以了解如何在不同的角度设置文本方向。通常,单元格内容从左到右水平显示,角度为 00;但是,如果需要,可以使用以下代码旋转文本方向。

Here you can learn how to set the text direction in different angles. Usually cell contents are displayed horizontally, from left to right, and at 00 angle; however you can use the following code to rotate the text direction, if required.

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);

      //30 degrees
      myStyle = workbook.createCellStyle();
      myStyle.setRotation((short) 30);
      cell = row.createCell(3);
      cell.setCellValue("30D angle");
      cell.setCellStyle(myStyle);

      //90 degrees
      myStyle = workbook.createCellStyle();
      myStyle.setRotation((short) 90);
      cell = row.createCell(5);
      cell.setCellValue("90D angle");
      cell.setCellStyle(myStyle);

      //120 degrees
      myStyle = workbook.createCellStyle();
      myStyle.setRotation((short) 120);
      cell = row.createCell(7);
      cell.setCellValue("120D angle");
      cell.setCellStyle(myStyle);

      //270 degrees
      myStyle = workbook.createCellStyle();
      myStyle.setRotation((short) 270);
      cell = row.createCell(9);
      cell.setCellValue("270D angle");
      cell.setCellStyle(myStyle);

      //360 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");
   }
}

将上述代码保留在 TextDirectin.java 文件中,然后从命令提示符编译并执行它,如下所示−

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

$javac TextDirection.java
$java TextDirection

它会编译并执行以生成在您当前目录中名为 textdirection.xlsx 的 Excel 文件,并在命令提示符上显示以下输出。

It will compile and execute to generate an Excel file named textdirection.xlsx in your current directory and display the following output on the command prompt.

textdirection.xlsx written successfully

textdirection.xlsx 文件如下所示−

The textdirection.xlsx file looks as follows −

textdirection