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="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="tableaction">
<html:text property="tablename"/>
<html:submit/>
</html:form>


</body>
</html>

and a Action Class
/*
* TableAction.java
*
* 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.*;
import java.io.*;
/**
*
* @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 {


response.setContentType("application/excel");
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(rs.next()) {
HSSFRow row = sheet.createRow((short) i);
HSSFCell cell1= row.createCell((short)1);
cell1.setCellValue(rs.getString(1));
HSSFCell cell2= row.createCell((short)2);
cell2.setCellValue(rs.getString(2));
HSSFCell cell3= row.createCell((short)3);
cell3.setCellValue(rs.getString(3));
i++;
}

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

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

}
}


With STRUTS FORM BEAN
/*
* TableBean.java
*
* 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() {
super();
// 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,

Jos
Oct 11 '07 #2

10K+
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.