wood burning stoves 2.0*
The moose likes JSP and the fly likes Exporting HTML Data to excel issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Exporting HTML Data to excel issue" Watch "Exporting HTML Data to excel issue" New topic
Author

Exporting HTML Data to excel issue

hari kemp
Greenhorn

Joined: Jan 04, 2012
Posts: 1

I am using the below code to get the data to an excel sheet. But unfortunately nothing is working for me. I am not getting any dialogbox upon clicking on the anchor links to save the output to .xlsx or doc format.

Please assist

<%@page import="java.io.File"%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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>Excel/Word Export</title>

</head>

<body>

<%

boolean isExcel = true;

if ("1".equals(request.getParameter("excel"))) {

isExcel = false;

}

boolean isWord = true;

if ("1".equals(request.getParameter("word"))) {

isWord = false;

}

%>

<%

if (!isExcel) {

String formName = "";

formName = "test.xlsx";

File filepath = new File(formName);

response.setContentType("application/vnd.ms-excel");

response.setHeader("Content-Disposition", "inline; filename=" + filepath);

}

if (!isWord) {

String formName = "";

formName = "test.doc";

File filepath = new File(formName);

response.setContentType("application/vnd.ms-word");

response.setHeader("Content-Disposition", "inline; filename=" + filepath);

}

%>

<table border="5" align="center">

<tr>

<th bgcolor="black" style="color: white;">Sr. No</th>

<th bgcolor="black" style="color: white;">Name</th>

<th bgcolor="black" style="color: white;">Address</th>

</tr>

<%

for (int i = 1; i <= 10; i++) {

%>

<tr>

<td><%=i%></td>

<td>Export to Excel</td>

<td>JSP Data</td>

</tr>

<%}%>

</table>

<div align="center">

<%if (isExcel || !isWord) {%>

<a href="test.jsp?excel=1">Export to Excel</a>

<%}%>

<%if (isWord || !isExcel) {%>

<a href="test.jsp?word=1">Export to Word</a>

<%}%>

</div>

</body>

</html>
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
It seems that you're under the impression that by setting a content type header you can make a JSP page emit a Word or Excel document. That's not the case - you need to use a library like Apache POI which can create both those file types.

I don't understand at all what you're trying to do with the File object you're creating, but since the whole approach won't work as is, that's probably a moot point.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61103
    
  66

And... it's 2012: 10 years after the introduction of JSP 2 with the EL and JSTL. The use of scriptlets in a JSP as if it were still 2001 is nothing short of irresponsible at this point.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
wood burning stoves
 
subject: Exporting HTML Data to excel issue