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 檔案如下所示 −

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 檔案如下所示 −

Text Direction
廣告