jasperreports 结合ireport生成pdf,word(jsp调用ireport)

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page import="net.sf.jasperreports.engine.JasperFillManager" %>
<%@ page import="net.sf.jasperreports.engine.JasperPrint"%>  
<%@ page import="net.sf.jasperreports.engine.JRException"%>
<%@ page import="zte.SQLConn"%>ger"%> 
<%@ page import="net.sf.jasperreports.engine.*" %>
<%
		System.out.println("----001.success jspreport");
		System.out.println("----001.success jspreport");
		System.out.println("----001.success jspreport");
		//String Name = "1";
		SQLConn sqlConn = new SQLConn();
		 try {

		Connection conn= sqlConn.getConnection();
		File exe_rpt = new File(application.getRealPath("jspreport/test.jasper"));
        Map parameters = new HashMap();   
       // parameters.put("Name", Name); 
		if(exe_rpt.exists()){
			System.out.println("yes,file");
		}else{
			System.out.println("no,file");
		}
		System.out.println("----002.success jspreport");
		System.out.println("----002.success jspreport");
		System.out.println("----002.success jspreport");
		System.out.println(exe_rpt.getPath());
        try {   


JasperPrint jasperPrint = JasperFillManager.fillReport(new FileInputStream(exe_rpt),parameters,conn);
JasperPrintManager.printReport(jasperPrint, false);

response.setContentType("application/msword"); 
conn.close();
           //	JasperPrint jasperPrint = JasperFillManager.fillReport(exe_rpt.getPath(),parameters,conn);

		   //byte[] bytes = JasperRunManager.runReportToPdf(exe_rpt.getPath(),parameters,conn);
		   // response.setContentType("application/pdf"); 
			//response.setContentLength(bytes.length); 
			//ServletOutputStream ouputStream = response.getOutputStream();
			//ouputStream.write(bytes,0,bytes.length);
			//释放在jsp中使用的对象,会调用response.getWriter(),因为这个方法是和response.getOutputStream()相冲突的!
			//使用完输出流以后
			out.clear(); 
            out = pageContext.pushBody();
            
//			ouputStream.flush(); 
//			ouputStream.close();
			conn.close();
		}catch(JRException ex){
			out.print("Jasper Output Error:"+ex.getMessage());
		}
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
%>




-------------------------调用javaBean做数据源

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@page import="java.sql.*"%>
<%@page import="net.sf.jasperreports.engine.JasperFillManager"%>
<%@page import="net.sf.jasperreports.engine.*"%>
<%@page import="net.sf.jasperreports.engine.util.*"%>
<%@page import="net.sf.jasperreports.engine.export.*"%>
<%@page import="net.sf.jasperreports.j2ee.servlets.*"%>
<%@page import="java.util.*"%>
<%@page import="java.util.List"%>
<%@page import="java.io.*"%>
<%@page import="net.sf.jasperreports.engine.JasperPrintManager"%>
<%@page import="net.sf.jasperreports.view.*"%>
<%@page import="java.sql.*, javax.sql.DataSource, javax.naming.InitialContext"%>
<%@page import="java.lang.*"%>

<%@ page import="net.sf.jasperreports.engine.JasperFillManager" %>
<%@ page import="net.sf.jasperreports.engine.JasperPrint"%>
<%@ page import="net.sf.jasperreports.engine.JasperRunManager"%>     
<%@ page import="net.sf.jasperreports.engine.JRExporterParameter"%>
<%@ page import="net.sf.jasperreports.engine.JRException"%>
<%@ page import="net.sf.jasperreports.engine.export.JRXlsExporterParameter"%>   
<%@ page import="net.sf.jasperreports.engine.export.JRXlsExporter"%> 
<%@page import ="net.sf.jasperreports.engine.JRExporter"%>
<%@ page import="net.sf.jasperreports.engine.export.JRRtfExporter"%> 
<%@page import ="com.lowagie.text.*" %>
<%@ page import="zte.SQLConn"%><%@ page import="zte.testVo"%>
<%@ page import="net.sf.jasperreports.engine.JasperRunManager"%> 
<%@ page import="net.sf.jasperreports.engine.*" %>
<%@ page import="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"%>



<%
//  InitialContext ctx = new InitialContext();
//  DataSource ds = (DataSource) ctx.lookup("java:/MSSQLDS");
//  Connection conn = ds.getConnection();
 
 SQLConn sqlConn = new SQLConn();
		 try {
 Connection conn= sqlConn.getConnection1();
  //String accNo ="200672395001";
 
  //String accNo = request.getParameter("accNo");
  File reportFile1 = new File(application.getRealPath("jspreport/javaBean.jasper"));
  if (!reportFile1.exists())
    throw new JRRuntimeException("Fidsfsfd. The report design must be compiled first");
  JasperReport jasperReport1 = (JasperReport) JRLoader.loadObject(reportFile1.getPath());
  Map parameters = new HashMap();
  parameters.put("id", 1);
  ///`--------------------
                List<testVo> data = new ArrayList<testVo>(); 
                testVo TestVo = new testVo(); 
               // TestVo.setId1(100);
               // TestVo.setName1("肥肥");
               // TestVo.setSex1("10");
               // TestVo.setAge1("202");
               
            List<testVo> list = new ArrayList<testVo>();
	     try {
			Statement  stmt = conn.createStatement();
			ResultSet  rslt = stmt.executeQuery("select * from PARTNER where partner_id = (select distinct d.supplier_id from PROCUREMENT_ITEM a ,BIDDING b ,AUCTION c,MATERIAL_PRICE d where a.procurement_item_id = b.procurement_item_id and b.bidding_id =c.bidding_id and  c.auction_id = d.auction_id)"); 
			while (rslt.next()) {
				testVo obj = new testVo();
				obj.setContactMan(rslt.getString("CONTACT_MAN"));
				obj.setContactTel(rslt.getString("contact_Tel"));
				obj.setBank(rslt.getString("bank"));
			    obj.setAccount(rslt.getString("account"));
				list.add(obj);
			}
			
			for(int i=0;i<list.size();i++){
				TestVo.setContactMan(list.get(i).getContactMan());
				TestVo.setContactTel(list.get(i).getContactTel());
				TestVo.setBank(list.get(i).getBank());
				TestVo.setAccount(list.get(i).getAccount());
			}
	
		} catch (SQLException e) {
			e.printStackTrace();
		} 
		
            data.add(TestVo); 
            JRDataSource dataSource  = new JRBeanCollectionDataSource(data);
  //--------------
  
  JasperPrint jasperPrint1 =
      JasperFillManager.fillReport(
          jasperReport1,
          null,
          dataSource
      );
  
  //response.setContentType("application/ms_word");
  JRExporter exporter = null;
  exporter = new JRRtfExporter();
  List reportlist = (List)new java.util.ArrayList();
  reportlist.add(jasperPrint1);
  // System.out.println(reportlist.size() + "list");
  byte[] bytes;
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  
  exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, reportlist);
  exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
  
  //qijia 
  exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint1);
 //exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);
  
  
  exporter.exportReport();
  // exporter.exportReportToStream();
  bytes = baos.toByteArray();
  // System.out.println(bytes+"bytes");
  if (bytes != null && bytes.length > 0) {
    try {
      //response.reset();
      response.setContentType("application/ms_word");
      response.setHeader("Content-disposition",
                         "attachment; filename=yourReport.doc");
      response.setContentLength(bytes.length);
      ServletOutputStream ouputStream = response.getOutputStream();
      ouputStream.write(bytes, 0, bytes.length);
      //ouputStream.flush();
      out.clear();
      out = pageContext.pushBody();
      
      ouputStream.close();
     // response.reset();
  
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
    finally {
      conn.close();
    }
  }
  
      } catch (SQLException e1) {
			e1.printStackTrace();
		}
%>

------------------------connction做数据源
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@page import="java.sql.*"%>
<%@page import="net.sf.jasperreports.engine.JasperFillManager"%>
<%@page import="net.sf.jasperreports.engine.*"%>
<%@page import="net.sf.jasperreports.engine.util.*"%>
<%@page import="net.sf.jasperreports.engine.export.*"%>
<%@page import="net.sf.jasperreports.j2ee.servlets.*"%>
<%@page import="java.util.*"%>
<%@page import="java.util.List"%>
<%@page import="java.io.*"%>
<%@page import="net.sf.jasperreports.engine.JasperPrintManager"%>
<%@page import="net.sf.jasperreports.view.*"%>
<%@page import="java.sql.*, javax.sql.DataSource, javax.naming.InitialContext"%>
<%@page import="java.lang.*"%>
<%@ page import="net.sf.jasperreports.engine.JasperPrint"%>
<%@ page import="net.sf.jasperreports.engine.JRExporterParameter"%>
<%@page import ="net.sf.jasperreports.engine.JRExporter"%>
<%@ page import="net.sf.jasperreports.engine.export.JRRtfExporter"%> 
<%@page import ="com.lowagie.text.*" %>
<%@ page import="zte.SQLConn"%><%@ page import="zte.testVo"%>
<%@ page import="net.sf.jasperreports.engine.JasperRunManager"%> 
<%@ page import="net.sf.jasperreports.engine.*" %>
<%@ page import="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"%>


<%
//  InitialContext ctx = new InitialContext();
//  DataSource ds = (DataSource) ctx.lookup("java:/MSSQLDS");
//  Connection conn = ds.getConnection();
 
 SQLConn sqlConn = new SQLConn();
		 try {
 Connection conn= sqlConn.getConnection();
  //String accNo ="200672395001";
 
  //String accNo = request.getParameter("accNo");
  File reportFile1 = new File(application.getRealPath("jspreport/conntion.jasper"));
  if (!reportFile1.exists())
    throw new JRRuntimeException("Fidsfsfd. The report design must be compiled first");
  JasperReport jasperReport1 = (JasperReport) JRLoader.loadObject(reportFile1.getPath());
  Map parameters = new HashMap();
  parameters.put("id", 1);
  JasperPrint jasperPrint1 =
      JasperFillManager.fillReport(
          jasperReport1,
          parameters,
          conn
      );
  //response.setContentType("application/ms_word");
  JRExporter exporter = null;
  exporter = new JRRtfExporter();
  List reportlist = (List)new java.util.ArrayList();
  reportlist.add(jasperPrint1);
  // System.out.println(reportlist.size() + "list");
  byte[] bytes;
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  
  exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, reportlist);
  exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
  
  //qijia 
  exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint1);
 //exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);
  
  exporter.exportReport();
  // exporter.exportReportToStream();
  bytes = baos.toByteArray();
  // System.out.println(bytes+"bytes");
  if (bytes != null && bytes.length > 0) {
    try {
      //response.reset();
      response.setContentType("application/ms_word");
      response.setHeader("Content-disposition",
                         "attachment; filename=yourReport.doc");
      response.setContentLength(bytes.length);
      ServletOutputStream ouputStream = response.getOutputStream();
      ouputStream.write(bytes, 0, bytes.length);
      //ouputStream.flush();
      out.clear();
      out = pageContext.pushBody();
      
      ouputStream.close();
     // response.reset();
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
    finally {
      conn.close();
    }
  }
      } catch (SQLException e1) {
			e1.printStackTrace();
		}
%>
----------------------包
commons-beanutils-1.7.jar
commons-collections-3.1.jar
iText-2.1.7.jar
iTextAsian.jar
jakarta-bcel-20050813.jar
JasperBabylon-1.0.0.jar
jasperreports-3.0.0.jar
jasperreports-extensions-1.3.1.jar
jasperreports-flash-2.0.5.jar
mysql-connector-java-3.1.11-bin.jar

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