Skip to content

Commit e2110e3

Browse files
committed
Support for JasperReports xlsx exporter
Issue: SPR-12843
1 parent 1a8c6fa commit e2110e3

File tree

3 files changed

+90
-2
lines changed

3 files changed

+90
-2
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/view/jasperreports/JasperReportsMultiFormatView.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,7 +27,7 @@
2727
import org.springframework.util.CollectionUtils;
2828

2929
/**
30-
* Jasper Reports view class that allows for the actual rendering format
30+
* JasperReports view class that allows for the actual rendering format
3131
* to be specified at runtime using a parameter contained in the model.
3232
*
3333
* <p>This view works on the concept of a format key and a mapping key.
@@ -53,6 +53,7 @@
5353
* <li>{@code html} - {@code JasperReportsHtmlView}</li>
5454
* <li>{@code pdf} - {@code JasperReportsPdfView}</li>
5555
* <li>{@code xls} - {@code JasperReportsXlsView}</li>
56+
* <li>{@code xlsx} - {@code JasperReportsXlsxView}</li> (as of Spring 4.2)
5657
* </ul>
5758
*
5859
* <p>The format key can be changed using the {@code formatKey} property.
@@ -100,6 +101,7 @@ public JasperReportsMultiFormatView() {
100101
this.formatMappings.put("html", JasperReportsHtmlView.class);
101102
this.formatMappings.put("pdf", JasperReportsPdfView.class);
102103
this.formatMappings.put("xls", JasperReportsXlsView.class);
104+
this.formatMappings.put("xlsx", JasperReportsXlsxView.class);
103105
}
104106

105107

@@ -119,6 +121,7 @@ public void setFormatKey(String formatKey) {
119121
* <li>{@code html} - {@code JasperReportsHtmlView}</li>
120122
* <li>{@code pdf} - {@code JasperReportsPdfView}</li>
121123
* <li>{@code xls} - {@code JasperReportsXlsView}</li>
124+
* <li>{@code xlsx} - {@code JasperReportsXlsxView}</li> (as of Spring 4.2)
122125
* </ul>
123126
*/
124127
public void setFormatMappings(Map<String, Class<? extends AbstractJasperReportsView>> formatMappings) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2002-2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.web.servlet.view.jasperreports;
18+
19+
import net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter;
20+
21+
/**
22+
* Implementation of {@code AbstractJasperReportsSingleFormatView}
23+
* that renders report results in XLSX format.
24+
*
25+
* <p><b>This class is compatible with classic JasperReports releases back until 2.x.</b>
26+
* As a consequence, it keeps using the {@link net.sf.jasperreports.engine.JRExporter}
27+
* API which got deprecated as of JasperReports 5.5.2 (early 2014).
28+
*
29+
* @author Rob Harrop
30+
* @author Juergen Hoeller
31+
* @since 4.2
32+
*/
33+
@SuppressWarnings({"deprecation", "rawtypes"})
34+
public class JasperReportsXlsxView extends AbstractJasperReportsSingleFormatView {
35+
36+
public JasperReportsXlsxView() {
37+
setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
38+
}
39+
40+
@Override
41+
protected net.sf.jasperreports.engine.JRExporter createExporter() {
42+
return new JRXlsxExporter();
43+
}
44+
45+
@Override
46+
protected boolean useWriter() {
47+
return false;
48+
}
49+
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2002-2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.web.servlet.view.jasperreports;
18+
19+
/**
20+
* @author Juergen Hoeller
21+
* @since 4.2
22+
*/
23+
public class JasperReportsXlsxViewTests extends AbstractJasperReportsViewTests {
24+
25+
@Override
26+
protected AbstractJasperReportsView getViewImplementation() {
27+
return new JasperReportsXlsxView();
28+
}
29+
30+
@Override
31+
protected String getDesiredContentType() {
32+
return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
33+
}
34+
35+
}

0 commit comments

Comments
 (0)