Download to Excel

From VYRE

Jump to: navigation, search
  • Export Configuration are a better alternative to this method but they can currently only be used for content items, not user data
  • You can only export to a file, which is sent to the user’s inbox. In other words, you can’t simply press a “Download Table to Excel” link and get a “Save As…” prompt.

So first I thought I’d need to do some custom programming to achieve what I had to do.

Not so.

It turns out it is very simple to set up a VYRE template that can take render Excel content. Because each template is essentially a JSP page, you can set the content type for the HTTP response, which is all you need in most cases. The following code illustrates a simple template that generates a four-cell Excel table, in a file called ‘my_export.xls’:

<% 
    String path = request.getRequestURI();
    if (path != null && path.indexOf("vyre4") == -1) {
        response.setContentType("application/vnd.ms-excel"); 
        response.setHeader("Content-disposition", "attachment; filename=my_export.xls");
    }
%>
<html>
<body>
<table>
    <tr>
        <td><b>First Value</b></td>
        <td><b>Second Value</b></td>
        <td><b>Total</b></td>
    </tr>
    <tr>
        <td><b>10</b></td>
        <td><b>20</b></td>
       <td><b>=Sum(A2:B2)</b></td>
    </tr>
</table>
</body>
</html>

The if-statement at the top is necessary if you don’t want to get a file prompt every time you open the template in the backend. As you may see from the code, you set up your spreadsheet as a simple HTML table. You can even include some simple Excel commands, like the SUM() shown here. To set up a more general Excel-template, add a content area like shown here:

<% 
    String path = request.getRequestURI();
    if (path != null && path.indexOf("vyre4") == -1) {
        response.setContentType("application/vnd.ms-excel"); 
        response.setHeader("Content-disposition", "attachment; filename=my_export.xls");
    }
%>
<%@ taglib uri="http://www.vyre.com/taglibs/vyre-templates-1.0" prefix="vyre-template" %>
<html>
<head>
    <vyre-template:head />
</head>
<body>
<vyre-template:content-area id="export_content" shared="false" />
</body>
</html>

This template can be reused for different configurations; you must add some type of portlet that will generate table output in the content area. Be careful not to include any line breaks at the top of the page, they seem to translate as row breaks in the Excel output.

Personal tools