- Home ›
- Apache POIでExcelを操作 ›
- 行
行の高さを設定
広告
行の高さを設定する方法を確認します。
行の高さを設定するにはRowインターフェースで用意されているsetHeightInPointsメソッドを使います。
void setHeightInPoints(float height)
Set the row's height in points. Parameters: height - the height in points. -1 resets to the default height
引数に高さを表すfloat型の値を指定します。単位はポイントです。引数に -1 を設定するとデフォルトの高さに設定されます。
なお現在のデフォルトの高さを取得するにはSheetインターフェースで用意されているgetDefaultRowHeightInPointsメソッドを使います。
float getDefaultRowHeightInPoints()
Get the default row height for the sheet (if the rows do not define their own height) in points. Returns: default row height in points
現在のデフォルトの高さをfloat型の値で返します。単位はポイントです。
実際の使い方は次のようになります。
Workbook wb = new HSSFWorkbook(); Sheet sheet = wb.createSheet(); Row row1 = sheet.createRow(0); Row row2 = sheet.createRow(1); row1.setHeightInPoints(20); row2.setHeightInPoints(2 * sheet.getDefaultRowHeightInPoints());
この場合、行の高さを1つは20ポイントに設定し、もう1つは現在のデフォルトの高さの2倍に設定しています。
サンプルプログラム
実際に試してみましょう。
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import java.io.*;
public class Sample4_1{
public static void main(String[] args){
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet();
Row row1 = sheet.createRow(1);
Row row3 = sheet.createRow(3);
row1.setHeightInPoints(60);
row3.setHeightInPoints(2 * sheet.getDefaultRowHeightInPoints());
FileOutputStream out = null;
try{
out = new FileOutputStream("sample4_1.xls");
wb.write(out);
}catch(IOException e){
System.out.println(e.toString());
}finally{
try {
out.close();
}catch(IOException e){
System.out.println(e.toString());
}
}
}
}
2つの行に対して、行の高さを設定しています。それでは作成されたファイルをExcelで開いてみます。
行の高さが変更されていることが確認できます。
( Written by Tatsuo Ikura )
著者 / TATSUO IKURA
これから IT 関連の知識を学ばれる方を対象に、色々な言語でのプログラミング方法や関連する技術、開発環境構築などに関する解説サイトを運営しています。