1 29 package net.sf.jasperreports.ebank; 30 31 import java.io.*; 32 import java.util.*; 33 import javax.servlet.*; 34 import javax.servlet.http.*; 35 36 37 41 public class XlsServlet extends HttpServlet 42 { 43 44 45 48 public void service( 49 HttpServletRequest request, 50 HttpServletResponse response 51 ) throws IOException, ServletException 52 { 53 byte[] bytes = null; 54 55 ReportBean reportBean = (ReportBean)request.getSession().getAttribute("reportBean"); 56 if (reportBean != null) 57 { 58 bytes = reportBean.getXls(); 59 } 60 61 if (bytes != null && bytes.length > 0) 62 { 63 response.setContentType("application/vnd.ms-excel"); 64 response.setHeader("Content-Disposition", "inline; filename=\"file.xls\""); 65 response.setContentLength(bytes.length); 66 ServletOutputStream ouputStream = response.getOutputStream(); 67 68 try 69 { 70 ouputStream.write(bytes, 0, bytes.length); 71 ouputStream.flush(); 72 } 73 catch (IOException e) 74 { 75 if (ouputStream != null) 76 { 77 try 78 { 79 ouputStream.close(); 80 } 81 catch (IOException ex) 82 { 83 } 84 } 85 } 86 } 87 else 88 { 89 response.setContentType("text/html"); 90 PrintWriter out = response.getWriter(); 91 out.println("<html>"); 92 out.println("<body bgcolor=\"white\">"); 93 out.println("<span class=\"bold\">Empty response.</span>"); 94 out.println("</body>"); 95 out.println("</html>"); 96 } 97 } 98 99 100 } 101 | Popular Tags |