您的位置:首页 > 编程语言 > ASP

IReport&Jasperreport使用实例代码

2007-07-25 14:07 423 查看
一..首先采用iReport作报表,生成相应的XXX..jrxml文件,具体操作可参考http://mooncome1983.blogchina.com/inc/iReport%23right.htm

二.采用Jasperreport在java中生成报表实例代码:


1.不连接数据库时:




public class Test ...{






/** *//**


* @param args


*/




public static void main(String[] args) ...{


// TODO Auto-generated method stub




...{


JasperReport jasperReport;


JasperPrint jasperPrint;




try ...{


String temp=System.getProperty("user.dir");


//compile xxx.jrxml


jasperReport = JasperCompileManager


.compileReport(temp+"/WebRoot/WEB-INF/classes/reports/jasperreports_demo.jrxml");


//路径的问题


jasperPrint = JasperFillManager.fillReport(jasperReport,


new HashMap(), new JREmptyDataSource());


//export pdf


JasperExportManager.exportReportToPdfFile(jasperPrint,


temp+"/WebRoot/WEB-INF/classes/reports/simple_report.pdf");


//export html


JasperExportManager.exportReportToHtmlFile(jasperPrint,


temp+"/WebRoot/WEB-INF/classes/reports/simple_report.html");






} catch (JRException e) ...{


e.printStackTrace();


}


}




}


}


2:连接数据库:




public class Test ...{






/** *//**


* @param args


*/




public static void main(String[] args) ...{


// TODO Auto-generated method stub




...{


JasperReport jasperReport;


JasperPrint jasperPrint;




try ...{


String temp=System.getProperty("user.dir");


//把.jrxml文件编译成.jasper文件,尽管也可直接访问的已生成的.jasper文件,不过不推荐这样做,毕竟是iReport使


//用的JasperReport版本可能与我们在Java工程中包含的版本不一样,这样,直接使用iReport生成的.jasper文件可能会


//在下一步的runReport中出问题。


jasperReport = JasperCompileManager


.compileReport(temp+"/WebRoot/WEB-INF/classes/reports/test1.jrxml");


//路径的问题


//fillReport( , , ) 最后一个参数是取得数据连接


JasperPrint jasperPrint1 = JasperFillManager.fillReport(jasperReport,new HashMap(),getMysqlConn());




//export pdf


JasperExportManager.exportReportToPdfFile(jasperPrint1,


temp+"/WebRoot/WEB-INF/classes/reports/test1.pdf");


//export html


JasperExportManager.exportReportToHtmlFile(jasperPrint1,


temp+"/WebRoot/WEB-INF/classes/reports/test1.html");






} catch (JRException e) ...{


e.printStackTrace();


}


}




}






public static Connection getMysqlConn()...{


String url="jdbc:mysql://localhost:3306/catalog";


Connection dcon = null;








try ...{


Class.forName("com.mysql.jdbc.Driver");


dcon=DriverManager.getConnection(url,"root","root");




} catch (Exception e) ...{


// TODO Auto-generated catch block




e.printStackTrace();


}




return dcon;




}




}






3.采用servlet直接生成pdf等文件






public class TestServlet extends HttpServlet ...{




public static final String XML_FILE_PATH =System.getProperty("user.dir")+"/WebRoot/WEB-INF/classes/reports/";




protected void doGet(HttpServletRequest req, HttpServletResponse res)




throws ServletException, IOException ...{




// 创建response输出流,设置responxe回应的头部


OutputStream out = res.getOutputStream();


res.setContentType("application/pdf");


res.setHeader("Content-Disposition","attachment; filename=test1.pdf"); //filename为生成PDF的文件名


String fileName = "test1.jrxml";


Map map = null;




// 调用createPdf()获得PDF输出的字节流并打印出来。


byte[] bytes = createPdf(fileName, map);


res.setContentLength(bytes.length);


out.write(bytes, 0, bytes.length);


out.flush();


}




protected void doPost(HttpServletRequest req, HttpServletResponse res)




throws ServletException, IOException ...{


doGet(req, res);


}




// 封装创建PDF输出的compile和run方法,返回最终生成的字节流






private byte[] createPdf(String fileName, Map map) ...{


String path = XML_FILE_PATH + fileName;//.jrxml文件的全路径


String jrFile = XML_FILE_PATH + "test1.jasper";//编译结果.jasper文件的全路径


byte[] bytes = null;




try...{


JasperCompileManager.compileReportToFile(path, jrFile);


//将.jrxml文件编译成.jasper文件




}catch (JRException e)...{


e.printStackTrace();


}




try...{


bytes =JasperRunManager.runReportToPdf(jrFile, map, getMysqlConn());


//生成相应的的字节流




}catch (JRException e)...{


e.printStackTrace();


}


return bytes;


}




public Connection getMysqlConn()...{


String url="jdbc:mysql://localhost:3306/catalog";


Connection dcon = null;




try ...{


Class.forName("com.mysql.jdbc.Driver");


dcon=DriverManager.getConnection(url,"root","root");




} catch (Exception e) ...{


// TODO Auto-generated catch block




try ...{


dcon.close();




} catch (SQLException e1) ...{


// TODO Auto-generated catch block


e1.printStackTrace();


}


e.printStackTrace();


}


return dcon;


}


}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: