01: /* ====================================================================
02: Licensed to the Apache Software Foundation (ASF) under one or more
03: contributor license agreements. See the NOTICE file distributed with
04: this work for additional information regarding copyright ownership.
05: The ASF licenses this file to You under the Apache License, Version 2.0
06: (the "License"); you may not use this file except in compliance with
07: the License. You may obtain a copy of the License at
08:
09: http://www.apache.org/licenses/LICENSE-2.0
10:
11: Unless required by applicable law or agreed to in writing, software
12: distributed under the License is distributed on an "AS IS" BASIS,
13: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: See the License for the specific language governing permissions and
15: limitations under the License.
16: ==================================================================== */
17:
18: package org.apache.poi.hssf.usermodel.examples;
19:
20: import org.apache.poi.hssf.usermodel.*;
21:
22: import java.io.FileOutputStream;
23: import java.io.IOException;
24: import java.util.Date;
25:
26: /**
27: * An example on how to cells with dates. The important thing to note
28: * about dates is that they are really normal numeric cells that are
29: * formatted specially.
30: *
31: * @author Glen Stampoultzis (glens at apache.org)
32: */
33: public class CreateDateCells {
34: public static void main(String[] args) throws IOException {
35: HSSFWorkbook wb = new HSSFWorkbook();
36: HSSFSheet sheet = wb.createSheet("new sheet");
37:
38: // Create a row and put some cells in it. Rows are 0 based.
39: HSSFRow row = sheet.createRow((short) 0);
40:
41: // Create a cell and put a date value in it. The first cell is not styled as a date.
42: HSSFCell cell = row.createCell((short) 0);
43: cell.setCellValue(new Date());
44:
45: // we style the second cell as a date (and time). It is important to create a new cell style from the workbook
46: // otherwise you can end up modifying the built in style and effecting not only this cell but other cells.
47: HSSFCellStyle cellStyle = wb.createCellStyle();
48: cellStyle.setDataFormat(HSSFDataFormat
49: .getBuiltinFormat("m/d/yy h:mm"));
50: cell = row.createCell((short) 1);
51: cell.setCellValue(new Date());
52: cell.setCellStyle(cellStyle);
53:
54: // Write the output to a file
55: FileOutputStream fileOut = new FileOutputStream("workbook.xls");
56: wb.write(fileOut);
57: fileOut.close();
58:
59: }
60: }
|