473,473 Members | 2,161 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

2 New Member
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
2 3888
JosAH
11,448 Recognized Expert MVP
I've moved your question to the Java Forum section where it belongs.

kind regards,

Jos
Oct 11 '07 #2
r035198x
13,262 MVP
Use code tags when posting code.
So what's the problem exactly? Are you getting errors/exceptions?
Oct 11 '07 #3

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

Similar topics

4
by: RK | last post by:
Hi, In my application, I need to copy data from an Excel file into a SQL table. The article related to this can be found at http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B306572 ...
3
by: DataBard007 | last post by:
My Access 97 database has a form that contains text boxes that display people's names, addresses, phone numbers, etc. The record source is a single table. I created a NextRecord and Previous...
1
by: Eric Keung | last post by:
Hi all, my case is I want to get an image from access database and I just know it's "OLE object" field type at access I also don't know how to insert it into access here is my code and it just...
2
by: Mustufa Baig | last post by:
Hi everybody, I have an ASP.NET website where clients can view their monthly billings by selecting different options. One of the option is the way they want to see the report i.e. whether they...
1
by: Mustufa Baig | last post by:
I have an ASP.NET website where I am showing off crystal reports to users by exporting them to pdf format. Following is the code: ---------------- 1 Private Sub ExportReport() 2 Dim oStream...
8
by: hyper-sensitive | last post by:
Hi Can some body suggest me which approach I should follow to import millions of records from data base into an excel sheet? I am working with ver. 2000 of MS-Excel . The requirement is to...
14
by: lmttag | last post by:
Hello. We're developing an ASP.NET 2.0 (C#) application and we're trying to AJAX-enable it. We're having problem with a page not showing the page while a long-running process is executing. So,...
4
LacrosseB0ss
by: LacrosseB0ss | last post by:
hello all; just wondering if this can be done. If so, I am looking for how to do it. I have a small Access macro that links 2 tables based on an address then assigns the address an ID based on the...
13
by: Peter | last post by:
VS2008 ans ASP.NET 3.5, Office 2003 What is the best way to run Excel Template from ASP.NET web page were the Excel is only installed on the client without any ActiveX? If so can someone point...
12
by: petter | last post by:
Hi! I have two questions: one question that regards the COUNT-function, and one about how to display a month even if I don’t have any data for that month. I have an Access database where I want...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.