インデント
広告
			インデントをスタイルに設定する方法を確認します。
インデントを設定するにはCellStyleインターフェースで用意されているsetIndentionメソッドを使います。
void setIndention(short indent)
set the number of spaces to indent the text in the cell Parameters: indent - - number of spaces
引数にはインデントの大きさをスペース文字の個数で指定します。
実際の使い方は次のようになります。
Workbook wb = new HSSFWorkbook(); DataFormat format = wb.createDataFormat(); CellStyle style = wb.createCellStyle(); style.setIndention((short)3);
この場合は、インデントとして3文字分を指定しています。
サンプルプログラム
実際に試してみましょう。
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import java.io.*;
public class Sample8_1{
  public static void main(String[] args){
    Workbook wb = new HSSFWorkbook();
    Sheet sheet = wb.createSheet();
    Row[] row = new Row[4];
    Cell[] cell = new Cell[4];
    for (int i = 0 ; i < 4 ; i++){
      row[i] = sheet.createRow(i + 1);
      cell[i] = row[i].createCell(2);
      cell[i].setCellValue("Coffee");
    }
    CellStyle style1 = wb.createCellStyle();
    style1.setIndention((short)1);
    style1.setAlignment(CellStyle.ALIGN_LEFT);
    cell[1].setCellStyle(style1);
    CellStyle style2 = wb.createCellStyle();
    style2.setIndention((short)2);
    style2.setAlignment(CellStyle.ALIGN_LEFT);
    cell[2].setCellStyle(style2);
    CellStyle style3 = wb.createCellStyle();
    style3.setIndention((short)3);
    style3.setAlignment(CellStyle.ALIGN_LEFT);
    cell[3].setCellStyle(style3);
    FileOutputStream out = null;
    try{
      out = new FileOutputStream("sample8_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());
      }
    }
  }
}
			各セルに対してインデントのスタイルを設定しています。それでは作成されたExcelファイルを開いてみます。
			
			
指定したインデントに応じて、表示が変わっていることが確認できます。
( Written by Tatsuo Ikura )
				著者 / TATSUO IKURA
これから IT 関連の知識を学ばれる方を対象に、色々な言語でのプログラミング方法や関連する技術、開発環境構築などに関する解説サイトを運営しています。