- Apache POI Tutorial
- Apache POI - Home
- Apache POI - Overview
- Apache POI - Java Excel APIs
- Apache POI - Environment
- Apache POI - Core Classes
- Apache POI - Workbooks
- Apache POI - Spreadsheets
- Apache POI - Cells
- Apache POI - Fonts
- Apache POI - Formula
- Apache POI - Hyperlink
- Apache POI - Print Area
- Apache POI - Database
- Apache POI Resources
- Apache POI - Questions & Answers
- Apache POI - Quick Guide
- Apache POI - Useful Resources
- Apache POI - Discussion
Apache POI - 字体
本章介绍如何在 Excel 电子表格中设置不同的字体、应用样式以及以不同方向角度显示文本。
每个系统都捆绑了大量的字体集合,例如 Arial、Impact、Times New Roman 等。如果需要,该集合还可以使用新字体进行更新。同样,字体可以有多种显示样式,例如粗体、斜体、下划线、删除线等。
字体和字体样式
以下代码用于将特定字体和样式应用于单元格的内容。
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的文件中。从命令提示符编译并执行它,如下所示 -
$javac FontStyle.java $java FontStyle
它会在当前目录中生成一个名为fontstyle.xlsx的 Excel 文件,并在命令提示符下显示以下输出。
fontstyle.xlsx written successfully
fontstyle.xlsx文件如下所示 -
文字方向
在这里您可以学习如何设置不同角度的文本方向。通常单元格内容是水平显示的,从左到右,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文件如下所示 -
