By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,269 Members | 1,561 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,269 IT Pros & Developers. It's quick & easy.

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

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

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ taglib uri="" prefix="bean" %>
<%@ taglib uri="" prefix="html" %>
<%@ taglib uri="" prefix="logic" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

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

<html:form action="tableaction">
<html:text property="tablename"/>


and a Action Class
* Created on August 10, 2007, 5:24 PM

package com.myapp.struts;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import java.sql.*;
import javax.sql.*;
* @author JavaUser
* @version

public class TableAction extends Action {

private final static String SUCCESS = "success";

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

TableBean tb= (TableBean)form;

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

DataSource ds = this.getDataSource(request);
Connection conn = ds.getConnection();
PreparedStatement ps= conn.prepareStatement("select * from "+tb.getTablename());
ResultSet rs = ps.executeQuery();
int i =0;

while( {
HSSFRow row = sheet.createRow((short) i);
HSSFCell cell1= row.createCell((short)1);
HSSFCell cell2= row.createCell((short)2);
HSSFCell cell3= row.createCell((short)3);


PrintWriter out = response.getWriter();
return null;
//return mapping.findForward("excelfile");


* Created on August 10, 2007, 5:20 PM

package com.myapp.struts;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

* @author JavaUser
* @version

public class TableBean extends org.apache.struts.action.ActionForm {

private String tablename;

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

public String getTablename() {
return tablename;

public void setTablename(String 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.
Oct 11 '07 #1
Share this Question
Share on Google+
2 Replies

Expert 10K+
P: 11,448
I've moved your question to the Java Forum section where it belongs.

kind regards,

Oct 11 '07 #2

P: 13,264
Use code tags when posting code.
So what's the problem exactly? Are you getting errors/exceptions?
Oct 11 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.