POI的使用[JAVA_Apache]POI套件的使用_產出EXCEL文件_儲存格的樣式使用方法(CellStyle)
使用儲存格 設定為日期格式
public static void training5() throws IOException {
Workbook wb = new HSSFWorkbook();
//or Workbook wb = new XSSFWorkbook();
CreationHelper createHelper = wb.getCreationHelper();
Sheet sheet = wb.createSheet("new sheet");
// Create a row
Row row = sheet.createRow(0);
// as a date.
Cell cell = row.createCell(0);
cell.setCellValue(new Date());
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setDataFormat(
createHelper.createDataFormat().getFormat("m/d/yy h:mm"));
cell = row.createCell(1);
cell.setCellValue(new Date());
cell.setCellStyle(cellStyle);
//you can also set date as java.util.Calendar
cell = row.createCell(2);
cell.setCellValue(Calendar.getInstance());
cell.setCellStyle(cellStyle);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
}
