Jasperreports与iReport的结合使用

package com.test;  
 
   
 
import java.awt.Dimension;  
import java.io.FileNotFoundException;  
import java.io.FileOutputStream;  
import java.io.OutputStream;  
import java.sql.Connection;  
import java.sql.DriverManager;  
import java.sql.ResultSet;  
import java.sql.SQLException;  
import java.sql.Statement;  
import java.util.ArrayList;  
import java.util.HashMap;  
import java.util.List;  
import java.util.Map;  
 
import net.sf.jasperreports.engine.JRException;  
import net.sf.jasperreports.engine.JRExporterParameter;  
import net.sf.jasperreports.engine.JasperCompileManager;  
import net.sf.jasperreports.engine.JasperExportManager;  
import net.sf.jasperreports.engine.JasperFillManager;  
import net.sf.jasperreports.engine.JasperPrint;  
import net.sf.jasperreports.engine.JasperReport;  
import net.sf.jasperreports.engine.export.JRXlsAbstractExporter;  
import net.sf.jasperreports.engine.export.JRXlsAbstractExporterParameter;  
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;  
import net.sf.jasperreports.view.JasperViewer;  
 
   
 
public class JasperTest {  
 
   
 
 public static void main(String[] args) {  
 
  swingView();  
 }  
 
   
 
 public static void swingView() {  
 
  try {  
   JasperReport jReport = JasperCompileManager  
     .compileReport("./jasper/duplicateReport.jrxml");  
   Map<String, String> parameters = new HashMap<String, String>();  
   parameters.put("searchCondition", "1=1");  
   // parameters.put("IS_IGNORE_PAGINATION", true);  
   JasperPrint jPrint = JasperFillManager.fillReport(jReport,  
     parameters, getOracleConnection());  
   JasperViewer viewer = new JasperViewer(jPrint);  
   viewer.setPreferredSize(new Dimension(200, 100));  
   viewer.setVisible(true);  
  } catch (JRException e) {  
   e.printStackTrace();  
  }  
 
 }  
 
   
 
 public static void exportToPdf() {  
  try {  
   JasperReport jReport = JasperCompileManager  
     .compileReport("./jasper/bugReport.jrxml");  
   Map<String, Boolean> parameters = new HashMap<String, Boolean>();  
   parameters.put("IS_IGNORE_PAGINATION", true);  
   JasperPrint jPrint = JasperFillManager.fillReport(jReport,  
     parameters, getConnection());  
   JasperExportManager.exportReportToPdfFile(jPrint,  
     "./jasper/bugReport.pdf");  
  } catch (JRException e) {  
 
   e.printStackTrace();  
  }  
 }  
 
   
 
 public static void exportToXls() {  
 
  try {  
   JasperReport jReport = JasperCompileManager  
     .compileReport("./jasper/bugReport.jrxml");  
   Map<String, Boolean> parameters = new HashMap<String, Boolean>();  
   parameters.put("IS_IGNORE_PAGINATION", true);  
   JasperPrint jPrint = JasperFillManager.fillReport(jReport,  
     parameters, getConnection());  
 
   List<JasperPrint> prints = new ArrayList<JasperPrint>();  
   prints.add(jPrint);  
   JRXlsAbstractExporter xlsExporter = new XlsPOIExport()  
     .getXlsExporter();  
   xlsExporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST,  
     prints);  
   OutputStream os = new FileOutputStream("./jasper/bugReport.xls");  
   xlsExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, os);  
   xlsExporter  
     .setParameter(  
       JRXlsAbstractExporterParameter.IS_ONE_PAGE_PER_SHEET,  
       false);  
   xlsExporter.setParameter(  
     JRXlsAbstractExporterParameter.IS_WHITE_PAGE_BACKGROUND,  
     false);  
   xlsExporter.setParameter(  
     JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,  
     Boolean.TRUE);  
 
   xlsExporter.setParameter(  
     JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,  
     Boolean.FALSE);  
 
   // 导出  
   xlsExporter.exportReport();  
  } catch (JRException e) {  
   e.printStackTrace();  
  } catch (FileNotFoundException e) {  
   e.printStackTrace();  
  }  
 }  
 
   
 
 public static void exportToOracleXls() {  
 
  try {  
   JasperReport jReport = JasperCompileManager  
     .compileReport("./jasper/duplicateReport.jrxml");  
   Map<String, String> parameters = new HashMap<String, String>();  
   parameters.put("searchCondition", "1=1");  
   // parameters.put("IS_IGNORE_PAGINATION", true);  
   JasperPrint jPrint = JasperFillManager.fillReport(jReport,  
     parameters, getOracleConnection());  
 
   List<JasperPrint> prints = new ArrayList<JasperPrint>();  
   prints.add(jPrint);  
   JRXlsAbstractExporter xlsExporter = new XlsPOIExport()  
     .getXlsExporter();  
   xlsExporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST,  
     prints);  
 
   xlsExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,  
     "./jasper/duplicateReport.xls");  
   xlsExporter.setParameter(  
     JRXlsAbstractExporterParameter.IS_ONE_PAGE_PER_SHEET, true);  
   xlsExporter.setParameter(  
     JRXlsAbstractExporterParameter.IS_WHITE_PAGE_BACKGROUND,  
     false);  
   xlsExporter.setParameter(  
     JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,  
     Boolean.TRUE);  
 
   xlsExporter.setParameter(  
     JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,  
     Boolean.FALSE);  
 
   // 导出  
   xlsExporter.exportReport();  
  } catch (JRException e) {  
   e.printStackTrace();  
  }  
 }  
 
   
 
 public static List<String> getTest() {  
 
  List<String> tests = new ArrayList<String>();  
  String sql = "select * from test";  
  Connection conn = null;  
  Statement stmt = null;  
  ResultSet rs = null;  
 
  try {  
   conn = getConnection();  
   stmt = conn.createStatement();  
   rs = stmt.executeQuery(sql);  
 
   while (rs.next()) {  
 
    tests.add(rs.getString("id"));  
    tests.add(rs.getString("name"));  
   }  
  } catch (SQLException e) {  
   e.printStackTrace();  
  }  
 
  return tests;  
 }  
 
   
 
 public static Connection getConnection() {  
 
  String driver = "com.mysql.jdbc.Driver";  
  String url = "jdbc:mysql://localhost/mysql";  
  String userName = "root";  
  String password = "root";  
 
  Connection conn = null;  
 
  try {  
   Class.forName(driver);  
   conn = DriverManager.getConnection(url, userName, password);  
  } catch (SQLException e) {  
   e.printStackTrace();  
  } catch (ClassNotFoundException e) {  
   e.printStackTrace();  
  }  
 
  return conn;  
 }  
 
   
 
 public static Connection getOracleConnection() {  
 
  Connection con = null;  
 
  try {  
 
   Class.forName("oracle.jdbc.driver.OracleDriver");  
 
   con = DriverManager.getConnection(  
     "jdbc:oracle:thin:@localhost:1521:miles", "system",  
     "mileschen");  
  } catch (Exception e) {  
   e.printStackTrace();  
  }  
 
  return con;  
 }  
 
 


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ming470612141/archive/2009/03/21/4012355.aspx

你可能感兴趣的:(java,oracle,sql,.net,mysql)