Apache-poi-fonts
提供:Dev Guides
Apache POI –フォント
この章では、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* ファイルは次のようになります。
テキストの方向
ここでは、さまざまな角度でテキストの方向を設定する方法を学ぶことができます。 通常、セルの内容は水平に、左から右に、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* ファイルは次のようになります。