Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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 :
java.io.*,
org.apache.poi.hssf.usermodel.HSSFSheet,
org.apache.poi.hssf.usermodel.HSSFCell,
org.apache.poi.hssf.usermodel.HSSFRow,
org.apache.poi.hssf.usermodel.HSSFCellStyle,
org.apache.poi.hssf.usermodel.HSSFFormat,
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")). 

createCellStyle():
This method is used to create the object of the HSSFCellStyle. This object is created to set the cell style. 

setDataFormat():
This method is used to set the format of the cellstyle.

getBuiltinFormat(): 
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/vnd.ms-excel" %>
<%page import="java.io.*" %>
<%page import="java.util.*" %>
<%
try{
   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();
    cellStyle.setDataFormat(HSSFDataFormat.
getBuiltinFormat
("m/d/yy h:mm"));
    cell = row.createCell((short)1);
    cell.setCellValue(new Date());
    cell.setCellStyle(cellStyle);
    FileOutputStream fileOut = new FileOutputStream
(
"c:\\excel\\dateExcel.xls");
    hwb.write(fileOut);
    fileOut.close();
  out.println("Your excel file has been generated");
            
        catch Exception ex ) {
          
        } 
%>

The output of the program is given below:

Download this example.

                         

» View all related tutorials
Related Tags: java c excel apache script import model setup io client method user print arguments type ip port sets anchor cli

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

org.apache.jasper.JasperException: /Date.jsp(9,0) Page directive: illegal to have multiple occurrences of contentType with different values (old: text/html; charset=ISO-8859-1, new: application/vnd.ms-excel)

Posted by Swati on Thursday, 09.4.08 @ 16:59pm | #77102

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.