package beans;
<p dir="auto">import java.util.HashMap;<br /> import java.util.List; <p dir="auto">import javax.activation.MimetypesFileTypeMap;<br /> import javax.servlet.ServletOutputStream;<br /> import javax.servlet.http.HttpServletResponse; <p dir="auto">import net.sf.jasperreports.engine.JRExporterParameter;<br /> import net.sf.jasperreports.engine.JasperCompileManager;<br /> import net.sf.jasperreports.engine.JasperFillManager;<br /> import net.sf.jasperreports.engine.JasperPrint;<br /> import net.sf.jasperreports.engine.JasperReport;<br /> import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;<br /> import net.sf.jasperreports.engine.export.JRCsvExporter;<br /> import net.sf.jasperreports.engine.export.JRPdfExporter;<br /> import net.sf.jasperreports.engine.export.ooxml.JRDocxExporter;<br /> import net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter; <p dir="auto">public class ExportData { <pre><code>public void exportAllFileData(String path,List list,String filetype,String filename,HttpServletResponse response) { System.out.print("File Type report: "+filetype); System.out.println("Main Export Method..."+filename); System.out.print("File Type report: "+filetype); JasperPrint jasperPrint; ServletOutputStream outputStream = null; try { JRBeanCollectionDataSource result=new JRBeanCollectionDataSource(list); JasperReport jasperReport = JasperCompileManager.compileReport(path); jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap<String,Object>(), result); System.out.print("-------------------- "); String mimeType = new MimetypesFileTypeMap().getContentType( filename+".pdf" ); System.out.println("mimetypee : "+mimeType); if(filetype.equals("PDF")) { response.setHeader("Content-Disposition", "attachment;filename"+filename+".pdf"); response.setContentType("application/octet-stream"); response.setContentLength(4096); outputStream = response.getOutputStream(); JRPdfExporter exporter = new JRPdfExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream); exporter.exportReport(); } if(filetype.equals("EXCEL")) { System.out.print("In Excel report: "); response.setHeader("Content-Disposition", "attachment;filename"+filename+".xlsx"); response.setContentType("application/octet-stream"); response.setContentLength(4096); outputStream = response.getOutputStream(); JRXlsxExporter exporter = new JRXlsxExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream); exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, filename+".xlsx"); exporter.exportReport(); } if(filetype.equals("DOC")) { response.setHeader("Content-Disposition", "attachment;filename"+filename+".docx"); response.setContentType("application/octet-stream"); response.setContentLength(4096); outputStream = response.getOutputStream(); JRDocxExporter exporter = new JRDocxExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream); exporter.exportReport(); } if(filetype.equals("CSV")) { response.setHeader("Content-Disposition", "attachment;filename"+filename+".csv"); response.setContentType("application/octet-stream"); response.setContentLength(4096); outputStream = response.getOutputStream(); JRCsvExporter exporter = new JRCsvExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream); exporter.exportReport(); } } catch(Exception e) { e.printStackTrace(); } } <p dir="auto">}
Congratulations @bhuvn! You have completed some achievement on Steemit and have been rewarded with new badge(s) :
<p dir="auto"><a href="http://steemitboard.com/@bhuvn" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link"><img src="https://images.hive.blog/768x0/https://steemitimages.com/70x80/http://steemitboard.com/notifications/votes.png" srcset="https://images.hive.blog/768x0/https://steemitimages.com/70x80/http://steemitboard.com/notifications/votes.png 1x, https://images.hive.blog/1536x0/https://steemitimages.com/70x80/http://steemitboard.com/notifications/votes.png 2x" /> Award for the number of upvotes <p dir="auto">Click on any badge to view your own Board of Honor on SteemitBoard.<br /> For more information about SteemitBoard, click <a href="https://steemit.com/@steemitboard" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">here <p dir="auto">If you no longer want to receive notifications, reply to this comment with the word <code>STOP <blockquote> <p dir="auto">By upvoting this notification, you can help all Steemit users. Learn how <a href="https://steemit.com/steemitboard/@steemitboard/http-i-cubeupload-com-7ciqeo-png" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">here!