Monday, October 28, 2019

Jasper Reports export to EXCEL, CSV and PDF Using JRXML Java and My SQL



This Post will explain , How to export different formats using Jasper and Jrxml using java

Step 1: Download Download Jasper Studio
or I Report Designer


Step 2 : Design your page by providing connection details and query details and what columns needs to be display in PDF/excel/CSV


Step 3: Your design Page details can be mentioned in Summary Band.






 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  
  
  
   
  
  
   
   
  
  
   
   
  
  
   
   
  
  
   
   
  
  
   
  
  
   
  
  
   
  
  
   
  
 
 
  
 
 
  
  
 
 
  
  
 
 
  
  
 
 
  
  
 
 
  
   
   
    
     
     
     
     
     
    
    
     
      
     
     
      
      
      
       
        
        
       
      
      
      
       
        
        
       
      
     
     
      
      
      
       
        
        
       
      
      
      
       
        
        
       
      
     
     
      
      
      
       
        
        
       
      
      
      
       
        
        
       
      
     
     
      
      
      
       
        
        
       
      
      
      
       
        
        
       
      
     
    
   
  
 






Step 4: Once design completes , then it is time to write java class to export into different formats


Step 5: Excel report Generation





package arrayListTest;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;

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.export.JRCsvExporter;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.export.SimpleCsvExporterConfiguration;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
import net.sf.jasperreports.export.SimplePdfExporterConfiguration;
import net.sf.jasperreports.export.SimpleWriterExporterOutput;
import net.sf.jasperreports.export.SimpleXlsReportConfiguration;

public class GenerateMultipleReportsFromJasper {
 
  public static void main(String[] args) {

  //Jrxml file source
  String sourceFileName = "C:\\Users\\Siva\\JaspersoftWorkspace\\csvpdf\\csv_export.jrxml";
  //destination file location
  String outXlsName = "C:\\Users\\Siva\\JaspersoftWorkspace\\csvpdf\\test.xls";
  //If we want to pass any parameters to jasper report, then we can use this map
  HashMap xlsParams = new HashMap();
  Connection con = null;
  try {
    Class.forName("com.mysql.jdbc.Driver");
    // here employee is database name, root is username and password
    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/employee", "root", "root");
    JasperReport report = JasperCompileManager.compileReport(sourceFileName );
    JasperPrint xlsPrint = JasperFillManager.fillReport(report, xlsParams, con);
    JRXlsExporter xlsExporter = new JRXlsExporter();
    xlsExporter.setExporterInput(new SimpleExporterInput(xlsPrint));
    xlsExporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outXlsName));
    SimpleXlsReportConfiguration xlsReportConfiguration = new SimpleXlsReportConfiguration();
    xlsReportConfiguration.setOnePagePerSheet(false);
    xlsReportConfiguration.setRemoveEmptySpaceBetweenRows(true);
    xlsReportConfiguration.setDetectCellType(false);
    xlsReportConfiguration.setWhitePageBackground(false);
    xlsExporter.setConfiguration(xlsReportConfiguration);
    xlsExporter.exportReport();
  } catch (Exception e) {
      System.out.println(e);
    }
    finally{
      try{
 if(con != null){
    con.close();
 }
 
      }
    catch(Exception ex){
 }
    }
  }

}


Step 6: CSV Generation




HashMap csvParamsMap = new HashMap();
String outcsvName = "C:\\Users\\Siva\\JaspersoftWorkspace\\csvpdf\\test.csv";
JasperReport report1 = JasperCompileManager.compileReport(sourceFileName );
JasperPrint jasperPrint = JasperFillManager.fillReport(report1, csvParamsMap, con);
JRCsvExporter exporter = new JRCsvExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleWriterExporterOutput(new File(outcsvName)));
SimpleCsvExporterConfiguration configuration = new SimpleCsvExporterConfiguration();
configuration.setWriteBOM(Boolean.TRUE);
configuration.setRecordDelimiter("\r\n");
exporter.setConfiguration(configuration);
exporter.exportReport();


Step 7: PDF Generation




HashMap pdfParamsMap = new HashMap();
String outPdfName = "C:\\Users\\Siva\\JaspersoftWorkspace\\csvpdf\\test.pdf";
JasperReport report2 = JasperCompileManager.compileReport(sourceFileName );
JasperPrint jasperPrint2 = JasperFillManager.fillReport(report2, pdfParamsMap, con);
JRPdfExporter pdfExporter = new JRPdfExporter();
pdfExporter.setExporterInput(new SimpleExporterInput(jasperPrint2));
pdfExporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outPdfName));
SimplePdfExporterConfiguration pdfConfiguration = new SimplePdfExporterConfiguration();
pdfConfiguration.setCreatingBatchModeBookmarks(true);
pdfExporter.setConfiguration(pdfConfiguration);




Step 8: Required jar file to execute this program



1. Itext 2.1.7.jar
2. commons-beanutils-1.9.2.jar
3. commons-logging-1.2.jar
4. commons-collections-3.2.1.jar
5. commons-lang-2.6.jar
6. commons-digester-1.6.jar
7. jasperreports-6.2.0.jar
8. mysql-connector-java-5.1.40.jar
9. poi-ooxml-3.16.jar
10. poi-3.12.jar



Step 9: Once We downloaded all jars then add into java build path.

Step 10: This is how we can export different format using jasper jrxml and Java.


31 comments:

  1. 1F00BF728FValerie24B23ADF67November 25, 2024 at 7:09 PM

    F8351AEC3B
    sanal şov

    ReplyDelete
  2. 360397C388
    İşletmenizin sosyal medya performansını artırmak için çeşitli stratejiler uygulayabilirsiniz. Örneğin, içeriklerinizi daha fazla kişiye ulaşması için instagram kaydetme hizmetlerinden faydalanabilirsiniz. Bu sayede paylaşımlarınızın görünürlüğü artar ve daha fazla etkileşim elde edebilirsiniz. Daha detaylı bilgi için instagram kaydetme hizmetini inceleyebilirsiniz.
    İnternet üzerinde videoların daha fazla izlenmesini sağlamak isteyen bazı kullanıcılar, çeşitli yöntemler denemektedir. Özellikle "shorts izlenme hilesi" gibi ifadelerle karşılaşmak mümkündür; bu da videolara sahte veya artırılmış izlenmeler eklemek anlamına gelir. Ancak, bu tür yöntemlerin hesabınıza zarar verebileceğini ve platformların kurallarını ihlal edebileceğini unutmamak gerekir. Daha sağlıklı ve kalıcı sonuçlar için içerik kalitenizi artırmaya odaklanmanız en doğrusu olur. Detaylar için ilgili siteye göz atabilirsiniz: shorts izlenme hilesi.
    Sosyal medya hesaplarınızın etkisini artırmak ve daha fazla görünürlük elde etmek istiyorsanız, güvenilir kaynaklardan Türk beğeni satın alabilirsiniz. Bu sayede içerikleriniz daha fazla kişiye ulaşarak etkileşimi yükseltebilir. Eğer bu konuda destek almak veya detaylı bilgi edinmek isterseniz, türk beğeni satın al seçeneğini değerlendirebilirsiniz. Bu yöntem, sosyal medya stratejilerinizi güçlendirmede etkili olabilir.
    İnternet üzerinde içerik üreticileri, daha geniş kitlelere ulaşmak için çeşitli yöntemler denemektedir. Bunlardan biri de, "youtube abone satın al" gibi hizmetlerle abone sayılarını artırmaktır. Bu sayede videolarının görünürlüğü yükselirken, yeni izleyicilere ulaşmak daha kolay hale gelir. Ancak, bu tür yöntemlerin uzun vadede etkili olup olmadığını dikkatli değerlendirmek önemlidir. Daha fazla bilgi için [buraya tıklayabilirsiniz](https://begenisatinal.com.tr/41/youtube-abone-satin-al).

    ReplyDelete

AddToAny

Contact Form

Name

Email *

Message *