Saturday, May 1, 2010

JSP Page Exporting to Excel

JSP Page Exporting to Excel

The JSP page directive has this contentType attribute that tells the browser what content type to expect of the requested page. By setting this attribute, you can instruct the browser what application to invoke to handle the page. For sending the currently displayed report to Excel, you need to:

  • Set the contentType:
    <%@ page contentType="application/vnd.ms-excel;
    charset=UTF-8" %>
  • Set the interactive property of the ReportBlox to false. By setting the report to be non-interactive, the resulting file will not include menu items from the context menus. This is done in similar way as in Saving as Static HTML to File System.

In your JSP file containing ReportBlox, assuming you have code like the following to call a separate JSP page for exporting the report to Excel and pass in the report's bloxName:

<head>
<%
String reportName = "myReport";
%>
<script>
var REPORT_NAME = "<%= reportName %>";
function toExcel() {
window.open( "excel.jsp?reportname=" + REPORT_NAME, REPORT_NAME +
"_excel", "height=400, width=600, scrollbars=yes, resizable=yes");
}
</script>
</head>

<body>
...
<a href="javascript:toExcel()" >Export to Excel</a>

<bloxreport:report id="report" bloxName="<%=reportName%>"
interactive="true" errors="true" >
...
</bloxreport:report>

excel.jsp looks as follows:

<!--Using the taglibs -->
<%@ taglib uri="bloxreporttld" prefix="bloxreport" %>
<!--Importing the associated java classes in order to use the APIs -->
<%@ page import="com.alphablox.blox.*" %>
<!-- Setting the contentType -->
<%@ page contentType="application/vnd.ms-excel; charset=UTF-8" %>

<%
//Set report to non-interactive mode so the menus don't get rendered
String reportName = (String)request.getParameter( "reportname" );
if( reportName != null ) {
BloxContext context = BloxContextFactory.getBloxContext(request, response);
ReportBlox reportBlox = (ReportBlox)context.getBlox( reportName );
if( reportBlox != null ) {
reportBlox.setInteractive(false);
} else {
out.println( "The report " + reportName + " does not exist." );
}
} else {
out.println( "This page was called without a report name." );
}
%>
<html>
<head>
<title><%= reportName %> Excel Report</title>
<%--In line the styles so they'll be saved if the page is saved and
you are not asked to be authenticated--%>
<style>
<jsp:include page="style/reportstyles.css" flush="true"/>
</style>
</head>
<body>

<%--The reportblox tag that follows causes the html of the report to be
rendered --%>
<bloxreport:report id="reportBlox" bloxName="<%=reportName%>" />
</body>
</html>

Notice that we use a JSP include statement to include an inline style sheet. Depending on the version of Excel and your operating system, using imported style sheet may result in the need to authenticate the user when Excel needs to access the style sheet. If you use the JSP include technique to inline a style sheet, note the following:

  • This style sheet should not import other style sheets as Excel cannot resolve the imports.
  • If you want to use the provided report.css style sheet at /AlphabloxServer/theme/, since this style sheet actually imports three other style sheets (styles.css for the report, dialog.css for the context menus, and errors.css for ErrorBlox), you should change the JSP include statement to use styles.css:
    <head>
    <style>
    <jsp:include page="/AlphabloxServer/theme/styles.css" flush="true"/>
    </style>
    </head>

    This ensures the report format and layout is preserved in Excel, no additional authentication is required, and Excel does not hang due to failure to resolve the style sheet import statements.



-

No comments:

Post a Comment