Wednesday, 21 November 2012

POI API : -Creating exel on serverside and downloading it on client side using J2EE

//POI API : -Creating excel on server side and downloading it on client side using J2EE

Step 1 :-  Dowload this poi-3.8-20120326.jar from apache website
Step 2 :-  Create a dynamic web project and place above jar in the lib folder
Step  3:-   Create a jsp with name createExcel.jsp as given bellow

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><%@page
    language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<html>
<head>
<title>createXlSheet</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<h3> Poc on creating and modifying excel sheet </h3>
<form action="DemoServlet">//link to our servlet which will create excell and give it to us
<input type="submit" value="Create-xlsheet">
</form>
</body>
</html>

Step 4:- In web.xml make this as a first jsp to be loaded as given bellow

  <?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <display-name>Demoplots</display-name>
    <welcome-file-list>
        <welcome-file>jsps/createXlSheet.jsp</welcome-file>//depending on your location of your jsp it will change
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
</web-app>

Step 5 :- Create the DemoServlet under the src folder as given bellow

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.WorkbookUtil;

/**
 * Servlet implementation class DemoServlet
 */
@WebServlet("/DemoServlet")
public class DemoServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public DemoServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        System.out.println("enterring here");
        //set the content type as excel so that it will download it as  excell
        response.setContentType("application/vnd.ms-excel");
         //create a excel
        Workbook wb = new HSSFWorkbook();
        //creating sheet1
        Sheet sheet1 = wb.createSheet("Demophase1");
        // Create a row and put some cells in it. Rows are 0 based.
        Row row = sheet1.createRow((short) 0);
        // Create a cell and put a value in it.
        Cell cell = row.createCell(0);
        cell.setCellValue(1);

        // Or do it on one line.
        row.createCell(1).setCellValue(1.2);
        row.createCell(2).setCellValue("This is a string");
        row.createCell(3).setCellValue(true);
       
       //Creating sheet2 with name Demophase2
        Sheet sheet2 = wb.createSheet("Demophase2");
        String safeName = WorkbookUtil.createSafeSheetName("[Demophase3*?]"); // returns
                                                                        // "
Demophase3  "
        Sheet sheet3 = wb.createSheet(safeName);
        ServletOutputStream out = response.getOutputStream();
        wb.write(out);
        out.flush();
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}
Step 6 :- Clean Build the project and run it

output :-




 now click on the Create-xlsheet 






The moment you click ok  you can see the excell sheet generated with three sheets and data only in first sheet

No comments:

Post a Comment

Custom single threaded java server

 package com.diffengine.csv; import java.io.*; import java.net.*; import java.util.Date; public class Server { public static void main(Str...