• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exporting HTML Data to excel issue

 
hari kemp
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>
 
Tim Moores
Bartender
Posts: 2839
46
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64964
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic