日付型の値をセルに設定

広告

日付型の値をセルに設定する場合、二つのメソッドが用意されています。

setCellValue(java.util.Date value):

Converts the supplied date to its equivalent Excel numeric value and sets that into 
the cell. 

Note - There is actually no 'DATE' cell type in Excel. In many cases (when entering date 
  values), Excel automatically adjusts the cell style to some date format, creating the 
  illusion that the cell data type is now something besides CELL_TYPE_NUMERIC. POI does 
  not attempt to replicate this behaviour. To make a numeric cell display as a date, use 
  setCellStyle(CellStyle) etc. 

Parameters:
  value - the numeric value to set this cell to. For formulas we'll set the precalculated 
    value, for numerics we'll set its value. For other types we will change the cell to 
    a numerics cell and set its value.

setCellValue(java.util.Calendar value):

Set a date value for the cell. Excel treats dates as numeric so you will need to format 
the cell as a date.

This will set the cell value based on the Calendar's timezone. As Excel does not support 
timezones this means that both 20:00+03:00 and 20:00-03:00 will be reported as the same 
value (20:00) even that there are 6 hours difference between the two times. This 
difference can be preserved by using setCellValue(value.getTime()) which will 
automatically shift the times to the default timezone. 

Parameters:
  value - the date value to set this cell to. For formulas we'll set the precalculated 
    value, for numerics we'll set its value. For othertypes we will change the cell to 
    a numeric cell and set its value.

基本的な使い方は数値や文字列などと同じなのですが、日付型の値をセルに設定した時にセルのタイプが数値タイプとして扱われてしまいます。その為、下記のように数値として表示されてしまいます。

日付型の値をセルに設定

日付型の値を設定した場合に表示を日付形式にするには適切なスタイルを設定する必要があります。

実際の使い方は次のようになります。

Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet();
Row row = sheet.createSheet(0);
Cell cell = row.createCell(0);

cell.setCellValue(new Date());

CreationHelper createHelper = wb.getCreationHelper();
CellStyle cellStyle = wb.createCellStyle();
short style = createHelper.createDataFormat().getFormat("yyyy/mm/dd h:mm");
cellStyle.setDataFormat(style);

cell.setCellStyle(cellStyle);

スタイルを設定した場合にどのように表示されるのかについては、この後のサンプルでご確認下さい。またスタイルの設定については別のページにて詳しく解説します。

サンプルプログラム

実際に試してみましょう。

Sample4_1.java

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import java.io.*;
import java.util.Calendar;
import java.util.Date;

public class Sample4_1{
  public static void main(String[] args){
    Workbook wb = new HSSFWorkbook();
    Sheet sheet = wb.createSheet();

    sheet.setColumnWidth(0, 4096);
    sheet.setColumnWidth(1, 4096);

    Row row1 = sheet.createRow(1);
    Row row2 = sheet.createRow(2);

    Cell cell1_0 = row1.createCell(0);
    Cell cell1_1 = row1.createCell(1);
    Cell cell2_0 = row2.createCell(0);
    Cell cell2_1 = row2.createCell(1);

    cell1_0.setCellValue(new Date());
    cell1_1.setCellValue(new Date());
    cell2_0.setCellValue(Calendar.getInstance());
    cell2_1.setCellValue(Calendar.getInstance());

    CreationHelper createHelper = wb.getCreationHelper();
    CellStyle cellStyle = wb.createCellStyle();
    short style = createHelper.createDataFormat().getFormat("yyyy/mm/dd h:mm");
    cellStyle.setDataFormat(style);

    cell1_1.setCellStyle(cellStyle);
    cell2_1.setCellStyle(cellStyle);

    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());
      }
    }
  }
}

プログラムを実行すると次のようなファイルが作成されます。

日付型の値をセルに設定

0列目のセルは単に日付型の値を設定したもの。1列目のセルは同じように値を設定してからスタイルを設定したものです。0列目の方は数値として表示されていることが確認できます。

( Written by Tatsuo Ikura )

Profile
profile_img

著者 / TATSUO IKURA

プログラミングや開発環境構築の解説サイトを運営しています。