Export Data using jasper reports

in #jasper7 years ago

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();
    }
}

}

Sort:  

What is this man
Can't even understand where you started 😂😂😂
I am also an Engineer (CSE).
Followed you

Coin Marketplace

STEEM 0.18
TRX 0.16
JST 0.029
BTC 76576.73
ETH 3043.84
USDT 1.00
SBD 2.62