项ç®åå éè¦å¨springmvcçåºç¡ä¸æ´åjasperreportsçææ¥è¡¨ãå
¶å®springmvcå·²ç»æä¾äºå¯¹jasperreportsçæ¯æï¼æè§springmvcéç¨çä¸ä¸ªæ¯è¾å¥½çæ¹å¼æ¯å°æ¥è¡¨ççæä½ä¸ºä¸ä¸ªviewå¤çï¼ä½æ¯éè¦å¯¹æ¯ä¸ç§æ¥è¡¨é
ç½®ä»çjasperreports模æ¿åè§å¾çæ å°ï¼è¿æ ·çè¯æ·»å æ¥è¡¨å¿
é¡»åæ´é
ç½®ï¼æ¯è¾éº»ç¦ï¼æ以èªå·±æ³äºä¸ä¸ªæ¹æ³æ¥é¿å
è¿ç§é
ç½®ï¼ä»£ç å¯ä»¥å¾å®¹æåspringæ´åèµ·æ¥ã
japserreportsçææ¥è¡¨åºæ¬æµç¨å
¶å®å°±æ¯æ ¹æ®ä¸ä¸ªæ¨¡æ¿åæ°æ®æºçæä¸ä¸ªä¸é´ç±»åï¼ç¶åå¯ä»¥å¨æ¤åºç¡ä¸å¯ä»¥å¯¼åºå ç§æ ¼å¼ãæçæ³æ³æ¯æä¾æ¹æ³ä¾springmvcçcontrollerè°ç¨äº§çä¸é´æ件ï¼ç¶åå¨viewéé¢å客æ·ç«¯å¯¼åºè¯·æ±çæ ¼å¼ã
é¦å
æ¯ReportPrintç±»ï¼å¾ç®åï¼åªæ¯å
å«ä¸ä¸ªJasperPrint对象ï¼æ¢ä¸è¿°çä¸é´æ件ï¼ï¼ä»£ç å¾ç®åï¼ä¸è§£é
01 public class ReportPrint {
02 JasperPrint jasperPrint = null;
03
04 public JasperPrint getJasperPrint() {
05 return jasperPrint;
06 }
07
08 public void setJasperPrint(JasperPrint jasperPrint) {
09 this.jasperPrint = jasperPrint;
10 }
11
12 }
æ¥ä¸æ¥å°±æ¯ReportCreaterç±»ï¼è¯¥ç±»å¯éè¿spring注å
¥å°å
¶ä»ç±»ä¸ï¼è°ç¨å®çcreateReportæ¹æ³
01 public class ReportCreater {
02 private static final Log logger = LogFactory.getLog(ReportCreater.class);
03 private String jasperReportPath = null;//æ¥è¡¨ç模æ¿æ件åæ¾è·¯å¾ï¼ç¸å¯¹classpathï¼éè¿spring注å
¥ï¼
04 /**
05 * jasperDesignMapä½ä¸ºä¸ä¸ªç¼åæ¥åå¨ç¼è¯åçJasperReport模æ¿
06 */
07 private Map<String, JasperReport> jasperDesignMap = newConcurrentHashMap<String, JasperReport>();
08
09 public void resetJasperDesignCache() {
10 jasperDesignMap.clear();
11 }
12
13 /**
14 * controllerè°ç¨è¯¥æ¹æ³æ¥äº§çReportPrint对象
15 */
16 public ReportPrint createReport(final String reportKey, final ResultSet rs, Map<String, ?> reportParams) throws ReportException {
17 try {
18 return _createReport(reportKey, rs, reportParams);
19 } catch (JRException e) {
20 logger.error(null, e);
21 throw new ReportException("产çæ¥è¡¨åºé" + reportKey);
22 }
23 }
24
25 private ReportPrint _createReport(final String reportKey, final ResultSet rs, Map<String, ?> reportParams) throws ReportException, JRException {
26 JasperReport jasperReport = getJasperReport(reportKey);
27 ReportPrint reportPrint = new ReportPrint();
28 JRResultSetDataSource resultSetDataSource = newJRResultSetDataSource(rs);
29 JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, reportParams, resultSetDataSource);
30 reportPrint.setJasperPrint(jasperPrint);
31
32 return reportPrint;
33 }
34
35 private JasperReport getJasperReport(final String reportKey) {
36 try {
37 return _getJasperReport(reportKey);
38 } catch (IOException e) {
39 logger.error(null, e);
40 throw new ReportException("å
³éæ件æµå¼å¸¸:" + reportKey);
41 } catch (JRException e) {
42 logger.error(null, e);
43 throw new ReportException("产çæ¥è¡¨å¼å¸¸:" + reportKey);
44 }
45 }
46
47 private JasperReport _getJasperReport(final String reportKey) throwsIOException, JRException {
48 JasperReport jasperReport = null;
49 if (jasperDesignMap.containsKey(reportKey)) {
50 jasperReport = jasperDesignMap.get(reportKey);
51 } else {
52 jasperReport = getJasperReportFromFile(final String reportKey);
53 jasperDesignMap.put(reportKey, jasperReport);
54 }
55
56 return jasperReport;
57 }
58
59 /**
60 * ä»æ¨¡æ¿æ件ç¼è¯è·å¾æ¨¡æ¿å¯¹è±¡
61 */
62 private JasperReport getJasperReportFromFile(final String reportKey) throws IOException, JRException {
63 String filePath = jasperReportPath + reportKey + ".jrxml";//å¾çäºåªæ¯æjrxmlç
64 InputStream jasperFileIS = null;
65 JasperReport jasperReport = null;
66
67 try {
68 jasperFileIS =this.getClass().getClassLoader().getResourceAsStream(filePath);
69 if (jasperFileIS == null) {
70 throw new ReportException("æ¥è¡¨æ件ä¸åå¨:" + filePath);
71 }
72
73 JasperDesign jasperDesign = JRXmlLoader.load(jasperFileIS);
74 jasperReport = JasperCompileManager.compileReport(jasperDesign);
75 } finally {
76 if (jasperFileIS != null) {
77 jasperFileIS.close();
78 }
79 }
80
81 return jasperReport;
82 }
83
84 public String getJasperReportPath() {
85 return jasperReportPath;
86 }
87
88 public void setJasperReportPath(String jasperReportPath) {
89 this.jasperReportPath = jasperReportPath;
90 }
91
92 public static void main(String[] argv) {
93
94 }
95
96 }
以ä¸å°±å¯ä»¥äº§çä¸é´æ件äºï¼æ¥ä¸æ¥å°±æ¯æç
§springçviewè§èåä¸ä¸ªå¯¼åºåç§æ ¼å¼çè§å¾å°±å¯ä»¥äº
01 public class ReportView extends AbstractView {
02 private static final Log logger = LogFactory.getLog(ReportView.class);
03 private static final String XLS = "xls";
04 private static final String PDF = "pdf";
05 private static final String CSV = "csv";
06 private static final String REPORT_NAME = "reportName";
07 private static final String FORMAT = "format";
08 private static final String REPORT_PRINT = "reportPrint";
09 private static final String HTML = "html";
10
11 private static Map<String, IReportFileExporter> EXPORTER_MAP =
12 new HashMap<String, IReportFileExporter>(4);
13
14 static {
15 EXPORTER_MAP.put(XLS, new ReportXlsExporter());
16 EXPORTER_MAP.put(PDF, new ReportPdfExporter());
17 EXPORTER_MAP.put(CSV, new ReportCsvExporter());
18 EXPORTER_MAP.put(HTML, new ReportHtmlExporter());
19 }
20
21 @Override
22 protected void renderMergedOutputModel(Map model, HttpServletRequest request,
23 HttpServletResponse response) {
24 String reportName = (String) model.get(REPORT_NAME);//æ¥è¡¨çæ件å
25 String format = (String) model.get(FORMAT);//æ¥è¡¨çæ ¼å¼pdf xls .....
26 ReportPrint reportPrint = (ReportPrint) model.get(REPORT_PRINT);//è¿å°±æ¯ä¹åçæçä¸é´æ件
27 response.setContentType("application/x-msdown;charset=utf-8");
28 try {
29 /* http头éçæ件åè²ä¼¼ä¸æ¯æutf-8ï¼gbkä¹ç±»çç¼ç ï¼éè¦è½¬æ¢ä¸ä¸
30 * å¦å¤åç°å¦æç¨new String(reportName.getBytes("UTF-8"), "iso-8859-1")çè¯ChromeåFFç
31 * ä¸è½½å¯¹è¯æ¡çæ件åæ¯æ£å¸¸çï¼IEå´æ¯ä¹±ç ï¼åªè½ç¨GBKææ£å¸¸
32 */
33 response.setHeader("Content-Disposition","attachment;filename=\"" +
34 new String(reportName.getBytes("GBK"),"iso-8859-1") + "\"");
35 } catch (UnsupportedEncodingException e) {
36 logger.error(null, e);
37 }
38 exportFile(reportPrint, format, response);
39 }
40
41 private void exportFile(ReportPrint reportPrint, String format, HttpServletResponse response) {
42 try {
43 _exportFile(reportPrint, format, response);
44 } catch (JRException e) {
45 logger.error("导åºæ¥è¡¨å¼å¸¸", e);
46 } catch (IOException e) {
47 logger.error(null, e);
48 }
49 }
50
51 private void _exportFile(ReportPrint reportPrint, String format, HttpServletResponse response) throws IOException, JRException {
52 OutputStream buffOS = null;
53
54 try {
55 buffOS = newBufferedOutputStream(response.getOutputStream());
56 IReportFileExporter exporter = null;
57
58 if (EXPORTER_MAP.containsKey(format)) {
59 exporter = EXPORTER_MAP.get(format);//è·åéè¦æ ¼å¼ç导åºç±»
60 exporter.export(reportPrint, buffOS);
61 } else {
62 logger.error("é误çæ¥è¡¨æ ¼å¼:" + format);
63 }
64 } finally {
65 if (buffOS != null) {
66 buffOS.close();
67 }
68 }
69 }
70
71 }
导åºå¨æ¯ä¸ä¸ªç®åçæ¥å£ï¼åç§æ ¼å¼åªè¦å®ç°exportæ¹æ³å°±å¯ä»¥äº
1 public interface IReportFileExporter {
2 public void export(ReportPrint reportPrint, OutputStream os) throwsJRException;
3 }
ç»ä¸ä¸ªå¯¼åºPDFæ ¼å¼çä¾åï¼å¾ç®å
01 public class ReportPdfExporter implements IReportFileExporter {
02
03 public void export(ReportPrint reportPrint, OutputStream os) throwsJRException {
04 JRPdfExporter exporter = new JRPdfExporter();
05 exporter.setParameter(JRExporterParameter.JASPER_PRINT, reportPrint.getJasperPrint());
06 exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, os);
07 exporter.exportReport();
08 }
09
10 }
温馨提示:内容为网友见解,仅供参考