Share on Google+Share on Google+

Assing Date into Cells

In this program, we are going to pass date into the cells in excel sheet using java .

assing Date into cells


In this program, we are going to pass date into the cells in  excel sheet using java .You have create any number of  cells and rows and also you can pass different  data  types into cells. 

The package we need to import is :*,
org.apache.poi.hssf.usermodel. HSSFWorkbook.

org.apache.poi.hssf.usermodel.HSSFCellStyle class is used to create an object of HSSFCellStyle. By using this style object we set the format of the cell data values. For this we use setDataFormate(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm")). 

This method is used to create the object of the HSSFCellStyle. This object is created to set the cell style. 

This method is used to set the format of the cellstyle.

This method is used to build the format which will be passed into setDataFormat() to set the format for cell style.

To add date into cells, create new date object and pass directly into  setCellValue() method. At last set these in cell style.

The code of the program is given below:

<%page import="org.apache.poi.hssf.usermodel.HSSFSheet"%>
<%page import="org.apache.poi.hssf.usermodel.HSSFWorkbook"%>
<%page import="org.apache.poi.hssf.usermodel.HSSFRow"%>
<%page import="org.apache.poi.hssf.usermodel.HSSFCell"%>
<%page import="org.apache.poi.hssf.usermodel.HSSFCellStyle"%>
<%page import="org.apache.poi.hssf.usermodel.HSSFDataFormat"%>
<%page contentType="application/" %>
<%page import="*" %>
<%page import="java.util.*" %>
 HSSFWorkbook hwb = new HSSFWorkbook();
  HSSFSheet sheet = hwb.createSheet("new sheet");
  HSSFRow row = sheet.createRow((short)0);
  HSSFCell cell = row.createCell((short)0);
  cell.setCellValue(new Date());
  HSSFCellStyle cellStyle = hwb.createCellStyle();
("m/d/yy h:mm"));
  cell = row.createCell((short)1);
  cell.setCellValue(new Date());
  FileOutputStream fileOut = new FileOutputStream
  out.println("Your excel file has been generated");
  catch Exception ex ) {

The output of the program is given below:

Download this example.



Posted on: May 19, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Assing Date into Cells   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image