16

Apache POIでExcelファイルに日付を日付形式で設定したい。アドレスバーでは mm/dd/YYYY、セルでは dd-mmm (数字の月と日の略称: 01-Jan) のように値が設定されます。

4

1 に答える 1

22

CellStyle入力する必要があるセルにa を適用できます。ここに私の過去の作品からのいくつかのコードスニペットがあります。完全ではありませんが、基本的な考え方を示しています:

Row row = sheet.createRow(0);
Cell cell = row.createCell((short) 0);
cell.setCellType(Cell.CELL_TYPE_NUMERIC);

SimpleDateFormat datetemp = new SimpleDateFormat("yyyy-MM-dd");
Date cellValue = datetemp.parse("1994-01-01 12:00");
cell.setCellValue(cellValue);

//binds the style you need to the cell.
CellStyle dateCellStyle = wb.createCellStyle();
short df = wb.createDataFormat().getFormat("dd-mmm");
dateCellStyle.setDataFormat(df);
cell.setCellStyle(dateCellStyle);
    

JDK の日付形式の詳細については、http: //docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.htmlを参照してください。

于 2013-01-14T07:57:30.403 に答える