1

I have a JSP page where I have some data in some tables. I want all the tables in the JSP to be exported to a excel file on click of a link on that page. Im currently using the following code to achieve that:

<a href="Result.jsp?exportToExcel=YES">Export to Excel</a>


String exportToExcel = request.getParameter("exportToExcel");
    if (exportToExcel != null
            && exportToExcel.toString().equalsIgnoreCase("YES")) {

        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "inline; filename="
                + "report.xls"); 
         } 
    }

The problem im facing is, this code exports all the data on the JSP(including geaders, footers etc) to the .xls file. But I do not want that to happen. I am interested only to get the tables in the JSP into the .xls file. Any ideas on how can i achieve that?

Surya Chandra
  • 1,561
  • 5
  • 27
  • 42

1 Answers1

0

Without having the code from your jsp, it is impossible to know how your code is structured. However, there are several ways to do this. One of the easiest would be to put if statements in the jsp as follows: First, make sure the tld is included.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

Next, around the headers and footers and anything else, insert if statements.

<c:if test="${empty requestScope.exportToExcel || !requestScope.exportToExcel.contains('YES') }">
    <%@ include file="header.jsp" %>
</c:if>

This way, the header or anything else inside the if block only gets rendered when it is not exporting to excel.

But really, I would recommend building an actual excel document with Apache POI Excel or Andy Khan's JExcel API

(Recommendations stolen from a BalusC answer on the subject. Any errors or bad code are mine.)

Community
  • 1
  • 1
Mike
  • 1,199
  • 1
  • 15
  • 24