How to display records from Database to excel format in Browser in JAVA-POI

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zoeabraham
    New Member
    • Oct 2007
    • 2

    How to display records from Database to excel format in Browser in JAVA-POI

    I am Using STRUTS1.4 in NetBeans5.5 IDE Here we have a JSP Page

    <%@page contentType="te xt/html"%>
    <%@page pageEncoding="U TF-8"%>
    <%@ taglib uri="http://jakarta.apache. org/struts/tags-bean" prefix="bean" %>
    <%@ taglib uri="http://jakarta.apache. org/struts/tags-html" prefix="html" %>
    <%@ taglib uri="http://jakarta.apache. org/struts/tags-logic" prefix="logic" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
    </head>
    <body>

    <html:form action="tableac tion">
    <html:text property="table name"/>
    <html:submit/>
    </html:form>


    </body>
    </html>

    and a Action Class
    /*
    * TableAction.jav a
    *
    * Created on August 10, 2007, 5:24 PM
    */

    package com.myapp.strut s;

    import javax.servlet.h ttp.HttpServlet Request;
    import javax.servlet.h ttp.HttpServlet Response;
    import org.apache.poi. hssf.usermodel. HSSFCell;
    import org.apache.poi. hssf.usermodel. HSSFRow;
    import org.apache.poi. hssf.usermodel. HSSFSheet;
    import org.apache.poi. hssf.usermodel. HSSFWorkbook;

    import org.apache.stru ts.action.Actio n;
    import org.apache.stru ts.action.Actio nForm;
    import org.apache.stru ts.action.Actio nMapping;
    import org.apache.stru ts.action.Actio nForward;
    import java.sql.*;
    import javax.sql.*;
    import java.io.*;
    /**
    *
    * @author JavaUser
    * @version
    */

    public class TableAction extends Action {


    private final static String SUCCESS = "success";


    public ActionForward execute(ActionM apping mapping, ActionForm form,
    HttpServletRequ est request, HttpServletResp onse response)
    throws Exception {


    response.setCon tentType("appli cation/excel");
    TableBean tb= (TableBean)form ;

    HSSFWorkbook wb = new HSSFWorkbook();
    FileOutputStrea m fileOut=new FileOutputStrea m("C:/Documents and Settings/JavaUser/WebExcel/workbook.xls");
    HSSFSheet sheet = wb.createSheet( tb.getTablename ());

    DataSource ds = this.getDataSou rce(request);
    Connection conn = ds.getConnectio n();
    PreparedStateme nt ps= conn.prepareSta tement("select * from "+tb.getTablena me());
    ResultSet rs = ps.executeQuery ();
    int i =0;

    while(rs.next() ) {
    HSSFRow row = sheet.createRow ((short) i);
    HSSFCell cell1= row.createCell( (short)1);
    cell1.setCellVa lue(rs.getStrin g(1));
    HSSFCell cell2= row.createCell( (short)2);
    cell2.setCellVa lue(rs.getStrin g(2));
    HSSFCell cell3= row.createCell( (short)3);
    cell3.setCellVa lue(rs.getStrin g(3));
    i++;
    }

    wb.write(fileOu t);
    fileOut.close() ;

    PrintWriter out = response.getWri ter();
    out.println(fil eOut);
    return null;
    //return mapping.findFor ward("excelfile ");

    }
    }


    With STRUTS FORM BEAN
    /*
    * TableBean.java
    *
    * Created on August 10, 2007, 5:20 PM
    */

    package com.myapp.strut s;

    import javax.servlet.h ttp.HttpServlet Request;
    import org.apache.stru ts.action.Actio nErrors;
    import org.apache.stru ts.action.Actio nMapping;
    import org.apache.stru ts.action.Actio nMessage;

    /**
    *
    * @author JavaUser
    * @version
    */

    public class TableBean extends org.apache.stru ts.action.Actio nForm {

    private String tablename;

    public TableBean() {
    super();
    // TODO Auto-generated constructor stub
    }

    public String getTablename() {
    return tablename;
    }

    public void setTablename(St ring tablename) {
    this.tablename = tablename;
    }


    }

    I have to get the result page directly in the Browser. Please Help me to Solve this Problem. I am using POI Library files to do this task.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    I've moved your question to the Java Forum section where it belongs.

    kind regards,

    Jos

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Use code tags when posting code.
      So what's the problem exactly? Are you getting errors/exceptions?

      Comment

      Working...