1 28 package net.sf.jasperreports.j2ee.servlets; 29 30 import java.io.ByteArrayOutputStream ; 31 import java.io.IOException ; 32 import java.io.OutputStream ; 33 import java.util.List ; 34 35 import javax.servlet.ServletException ; 36 import javax.servlet.ServletOutputStream ; 37 import javax.servlet.http.HttpServletRequest ; 38 import javax.servlet.http.HttpServletResponse ; 39 40 import net.sf.jasperreports.engine.JRException; 41 import net.sf.jasperreports.engine.JRExporterParameter; 42 import net.sf.jasperreports.engine.export.JRXlsAbstractExporter; 43 import net.sf.jasperreports.engine.export.JRXlsAbstractExporterParameter; 44 45 49 public abstract class AbstractXlsServlet extends BaseHttpServlet 50 { 51 52 53 56 public void service( 57 HttpServletRequest request, 58 HttpServletResponse response 59 ) throws IOException , ServletException 60 { 61 List jasperPrintList = BaseHttpServlet.getJasperPrintList(request); 62 63 if (jasperPrintList == null) 64 { 65 throw new ServletException ("No JasperPrint documents found on the HTTP session."); 66 } 67 68 Boolean isBuffered = Boolean.valueOf(request.getParameter(BaseHttpServlet.BUFFERED_OUTPUT_REQUEST_PARAMETER)); 69 if (isBuffered.booleanValue()) 70 { 71 JRXlsAbstractExporter exporter = getXlsExporter(); 72 exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList); 73 74 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 75 exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos); 76 77 exporter.setParameter(JRXlsAbstractExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE); 78 exporter.setParameter(JRXlsAbstractExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE); 79 80 try 81 { 82 exporter.exportReport(); 83 } 84 catch (JRException e) 85 { 86 throw new ServletException (e); 87 } 88 89 byte[] bytes = baos.toByteArray(); 90 91 if (bytes != null && bytes.length > 0) 92 { 93 response.setContentType("application/xls"); 94 response.setHeader("Content-Disposition", "inline; filename=\"file.xls\""); 95 response.setContentLength(bytes.length); 96 ServletOutputStream ouputStream = response.getOutputStream(); 97 98 try 99 { 100 ouputStream.write(bytes, 0, bytes.length); 101 ouputStream.flush(); 102 } 103 finally 104 { 105 if (ouputStream != null) 106 { 107 try 108 { 109 ouputStream.close(); 110 } 111 catch (IOException ex) 112 { 113 } 114 } 115 } 116 } 117 } 128 else 129 { 130 response.setContentType("application/xls"); 131 response.setHeader("Content-Disposition", "inline; filename=\"file.xls\""); 132 133 JRXlsAbstractExporter exporter = getXlsExporter(); 134 exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList); 135 136 OutputStream ouputStream = response.getOutputStream(); 137 exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream); 138 139 exporter.setParameter(JRXlsAbstractExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE); 140 exporter.setParameter(JRXlsAbstractExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE); 141 142 try 143 { 144 exporter.exportReport(); 145 } 146 catch (JRException e) 147 { 148 throw new ServletException (e); 149 } 150 finally 151 { 152 if (ouputStream != null) 153 { 154 try 155 { 156 ouputStream.close(); 157 } 158 catch (IOException ex) 159 { 160 } 161 } 162 } 163 } 164 } 165 166 167 170 protected abstract JRXlsAbstractExporter getXlsExporter(); 171 172 173 } 174 | Popular Tags |