Exporting using jasper reports - PDF, Excel, CSV and Doc
This code about to export pdf excel and doc file using java
technology used:
java
jasperreports
spring
hibernate
package beans;
import java.util.HashMap;
import java.util.List;
import javax.activation.MimetypesFileTypeMap;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRCsvExporter;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.export.ooxml.JRDocxExporter;
import net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter;
public class ExportData {
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();
}
}
}