开发手册 欢迎您!
软件开发者资料库

Apache POI - 字体

Apache POI字体 - 从简单和简单的步骤学习Apache POI,从基本到高级概念,包括概述,Java Excel API的风格,安装,类和方法,工作簿,电子表格,单元格,字体和文本,公式,超链接,PrintArea和数据库交互。

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

每个系统都捆绑了大量字体,如如Arial,Impact,Times New Roman等.如果需要,也可以使用新字体更新集合.类似地,有各种样式可以显示字体,例如粗体,斜体,下划线,穿透等.

字体和字体样式

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

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 = 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(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");   } }


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

$javac FontStyle.java$java FontStyle


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

fontstyle.xlsx written successfully


fontstyle.xlsx 文件显示为如下:

FontStyle

文字方向

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

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 文件中,然后编译并执行从命令提示符如下.

$javac TextDirection.java$java TextDirection


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

textdirection.xlsx written successfully


textdirection.xlsx 文件看起来如下.

TextDirectin