日期:2014-05-18  浏览次数:20678 次

java web中做自定义报表怎么做?
我在网上搜了下,大概有ireport&jasperreport和jfreereport两种,但是介绍怎么用的资料很少,谁做过这方面的东东烦劳您帮我一下,谢谢!
分不够再加

------解决方案--------------------
用ireport制作要打印的文件,编译生成*.jasper(例如:classic.jasper)文件,把该文件放在jasperreport中report文件夹中,然后套在下面的程序:

<%@ page contentType= "text/html; charset=gb2312 "%>
<%@ page import= "javax.naming.Context "%>
<%@ page import= "javax.sql.DataSource "%>
<%@ page import= "javax.naming.InitialContext "%>
<%@ page import= "java.sql.* "%>
<%@ page import= "javax.sql.* "%>

<%@ page errorPage= "error.jsp " %>
<%@ 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.io.* " %>


<%
Connection conn;
try{
Context initCtx = new InitialContext();
Context envCtx=(Context) initCtx.lookup( "java:comp/env ");
//从Context中lookup数据源
ds=(DataSource)envCtx.lookup( "DbConn ");
conn=ds.getConnection();
}catch(Exception ne){
out.println(ne);
}


File reportFile = new File(application.getRealPath( "/reports/classic.jasper "));
if (!reportFile.exists())
throw new JRRuntimeException( "File Test.jasper not found. The report design must be compiled first. ");

JasperReport jasperReport = (JasperReport)JRLoader.loadObject(reportFile.getPath());

Map parameters = new HashMap();
parameters.put( "ReportTitle ", "Address Report ");
parameters.put( "BaseDir ", reportFile.getParentFile());

JasperPrint jasperPrint =
JasperFillManager.fillReport(
jasperReport,
parameters,
conn
);

JRHtmlExporter exporter = new JRHtmlExporter();

StringBuffer sbuffer = new StringBuffer();

session.setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);

exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);
exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "../servlets/image?image= ");

exporter.exportReport();

conn.close();

%>

注意:
"DbConn "是数据源的名字,
"/reports/classic.jasper "这个路径一定要正确

------解决方案--------------------
up