• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Exporting HTML Data to excel issue

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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>
 
Saloon Keeper
Posts: 5868
147
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 67299
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Humans and their filthy friendship brings nothing but trouble. My only solace is this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!