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;
}
}
二.采用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;
}
}
相关文章推荐
- 彩色的命令行 —— 使用 ANSI 色彩代码(export PS1='\[\e[1;32m\][\u@\h \w]\$\[\e[0m\] ')
- Node.js使用NodeMailer发送邮件实例代码
- 使用不同数据类型实例化Stack泛型类的代码
- 在Golang中使用C语言代码实例
- Android Fragments 使用详解附代码实例
- Android使用ViewDragHelper实现QQ6.X最新版本侧滑界面效果实例代码
- Vue2 使用 Echarts 创建图表实例代码
- 使用Python监控文件内容变化代码实例
- PHP Session 变量的使用方法详解与实例代码
- C++ Const 使用总结,代码实例亲测
- Ireport+Jasperreport实现报表使用
- 基于php&mysql实现聊天室功能的代码实例分享
- Hadoop的Partitioner使用实例2<转>
- Android绘图实例(Bitmmap,Canvas,Pain的使用)&动态的在图片上添加文字(canvas.drawText)
- Control Study->AdRotator广告控件显示自定义配置文件中广告以及与DataGrid控件结合使用显示图片(示例代码下载)
- 使用Java进行SNMP编程-SNMP4J-SNMPv1/v2-代码实例
- java正则表达式语法详解及其使用代码实例
- log4net 使用学习 及代码实例(转载的)
- Eclipse下使用GDT插件无法登陆GAE & GDT无法上传JAVA代码
- java正则表达式语法详解及其使用代码实例 (转)