参考:[color=red]ireport导出各种格式(pdf,excel,word,html,print)的例子[/color]:
[url]http://mingxiao2010.blog.163.com/blog/static/861904812010665366872/[/url]
说明:
[b]net.sf.jasperreports.engine.JRXlsExporter.setParameter常用参数说明[/b]
原文地址:生成例子[url]http://hi.baidu.com/szmneo/blog/item/cbb4cb22d07565469822edce.html[/url]
commons-collections-3.2.jar
commons-logging.jar
jasperreports-3.7.6.jar
jasperreports-applet-3.7.6.jar
commons-digester-2.1.jar
commons-beanutils-1.7.jar
mysql-connector-java-5.0.5.jar
poi-contrib-3.5-beta5-20090219.jar
poi-ooxml-3.5-beta5-20090219.jar
poi-scratchpad-3.5-beta5-20090219.jar
iTextAsian.jar
poi-3.7-20101029.jar
itext-2.1.7.jar
注意后面两个红色的,第一,jasper3.7必须用poi3.6以上;第二,itext必须也是2.1.7以上,关键是itext官方的不能用,⊙﹏⊙b汗,要从jasper下载的包里找到lib,然后用里面的itext才行……
好了,开始代码:ireport设计jasper文件的就不说了
遇到的问题:poi导致错误的忘记了,O(∩_∩)O哈哈~
Exception in thread "main" java.lang.NoClassDefFoundError: com/lowagie/text/DocumentException
at net.sf.jasperreports.engine.JasperExportManager.exportReportToPdfFile(JasperExportManager.java:121)
at jasperreportsdemoapp.Main.main(Main.java:67)
..............
这个就是开始说的itext包不正确导致的参考下面的网站解决掉了,呵呵
参考:http://blogs.sun.com/geertjan/entry/itext_and_jasper_using_them
其中注意一下提示的错误NoClassDefFoundError,并非是说你没有添加相关的jar包到classpath,而极可能是版本不对,参阅下面:
Difference between ClassNotFoundException and NoClassDefFoundError
http://www.jroller.com/sjivan/entry/difference_between_classnotfoundexception_and_noclassdeffounderror
最后提一下,jasper现在使用需要辅助jar包的情况:
JasperReports Requirements
http://jasperforge.org/website/jasperreportswebsite/trunk/documentation.html?header=project&target=jasperreports
[color=red]w[b]eb中应用jasperreport[/b][/color]:
[url]http://923080512.iteye.com/blog/1396484[/url]
在web中使用jasperreport 要导入的包:
jasperreports-4.5.0.jar
js_activation-1.1.jar
iText-2.1.7.jar
iTextAsian.jar
groovy-all-1.7.5.jar
commons-beanutils-1.8.2.jar
commons-digester-1.7.jar
commons-collections-3.2.1.jar
poi-3.7-20101029.jar
方法1:
方法2:
[url]http://mingxiao2010.blog.163.com/blog/static/861904812010665366872/[/url]
说明:
PDF: JRAbstractExporter exporter =new JRPdfExporter();
Excel: JRAbstractExporter exporter = new JRXlsExporter();
HTML: JRAbstractExporter exporter = new JRHtmlExporter();
Word: JRAbstractExporter exporter = new JRRtfExporter();
[b]net.sf.jasperreports.engine.JRXlsExporter.setParameter常用参数说明[/b]
JRExporterParameter. JASPER_PRINT
//这个参数是JasperPrint的对象,exporter在导出报表前会做检测
JRExporterParameter. JASPER_PRINT_LIST
//这个参数是包含了n个JasperPrint对象的java.util.List,exporter在导出报表前会做检测
JRExporterParameter.INPUT_STREAM
//这个参数是JasperPrint被序列化对象的输入流,exporter在导出报表前会做检测
JRExporterParameter.INPUT_URL
//这个参数是包含JasperPrint被序列化对象的URL,exporter在导出报表前会做检测
JRExporterParameter.INPUT_FILE_NAME
这个参数是存储了JasperPrint被序列化对象的文件路径,exporter在导出报表前会做检测
注意:以上几个参数不能全部为空
JRExporterParameter.OUTPUT_STRING_BUFFER
//这个参数是java.lang.StringBuffer的对象,存储已经产生出的指定格式报表的内容
JRExporterParameter.OUTPUT_WRITER
//这个参数是java.io.Writer的对象,将指定格式报表的内容发送到一个字符流,例如Servlet的PrintWriter
JRExporterParameter.OUTPUT_STREAM
//这个参数是java.io.OutputStream的对象,将指定格式报表的内容发送到一个输出流,例如ServletOutputStream
JRExporterParameter.OUTPUT_FILE
//这个参数是java.io.FILE的对象,将指定格式报表的内容存储到文件里面
JRExporterParameter.OUTPUT_FILE_NAME
//这个参数是java.lang.String的对象,将指定格式报表的内容存储到文件里面
JRExporterParameter.CHARACTER_ENCODING
//这个参数是java.lang.String的对象,指定格式报表的内容编码
JExcelApiExporterParameter.IS_FONT_SIZE_FIX_ENABLED
//这个参数是java.lang.Boolean的对象,是否允许自动修正Excel每个栏位的大小
JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET
//这个参数是java.lang.Boolean的对象,每一页是否用一个Sheet
JRXlsExporterParameter. IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS
//这个参数是java.lang.Boolean的对象,是否移除行与行之间的空行
JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND
//这个参数是java.lang.Boolean的对象,页面的背景是否为白的
JRXlsExporterParameter.SHEET_NAMES
//这个参数是java.lang.String的对象,Sheet的名字
JRCsvExporterParameter.FIELD_DELIMITER
//这个参数是java.lang.String的对象,栏位之间的分隔符
JRCsvExporterParameter.RECORD_DELIMITER
//这个参数是java.lang.String的对象,栏位之间的分隔符
JRHtmlExporterParameter.IS_OUTPUT_IMAGES_TO_DIR
//这个参数是java.lang.Boolean的对象,是否输出图片到目录
JRHtmlExporterParameter.IMAGES_DIR_NAME
//这个参数是java.lang.String的对象,图片目录的绝对路径
JRHtmlExporterParameter.IMAGES_DIR
//这个参数是java.io.File的对象,图片目录
JRHtmlExporterParameter.IMAGES_URI
//这个参数是java.lang.String的对象,通过Web访问时图片的URI
原文地址:生成例子[url]http://hi.baidu.com/szmneo/blog/item/cbb4cb22d07565469822edce.html[/url]
commons-collections-3.2.jar
commons-logging.jar
jasperreports-3.7.6.jar
jasperreports-applet-3.7.6.jar
commons-digester-2.1.jar
commons-beanutils-1.7.jar
mysql-connector-java-5.0.5.jar
poi-contrib-3.5-beta5-20090219.jar
poi-ooxml-3.5-beta5-20090219.jar
poi-scratchpad-3.5-beta5-20090219.jar
iTextAsian.jar
poi-3.7-20101029.jar
itext-2.1.7.jar
注意后面两个红色的,第一,jasper3.7必须用poi3.6以上;第二,itext必须也是2.1.7以上,关键是itext官方的不能用,⊙﹏⊙b汗,要从jasper下载的包里找到lib,然后用里面的itext才行……
好了,开始代码:ireport设计jasper文件的就不说了
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRHtmlExporter;
import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
import net.sf.jasperreports.view.JasperViewer;
public class TestIreport {
public String jasperPath = "F:/测试/report2.jasper";
public static void main(String[] args) throws Exception {
TestIreport ti = new TestIreport();
// ti.getReportHTML();
ti.getReportPdf();
//ti.getReportExcel();
}
public void getReportPdf() throws Exception {
Map<String, Object> param = new HashMap<String, Object>();
param.put("REPORT_CONNECTION", getConn());
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperPath,
param);
JRPdfExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,
"F:/测试/report2.pdf");
exporter.exportReport();
}
public void getReportExcel() throws Exception {
Map<String, Object> param = new HashMap<String, Object>();
param.put("REPORT_CONNECTION", getConn());
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperPath,
param);
File destFile = new File(
"F:/测试/report2.xls");
JRXlsExporter exporter = new JRXlsExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile
.toString());
exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,
true);
exporter.exportReport();
}
public void getReportHTML() throws Exception {
Map<String, Object> param = new HashMap<String, Object>();
param.put("REPORT_CONNECTION", getConn());
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperPath,
param);
// 使用 JRExporter 来生成 html , 很多参数可以查 api 或 ireport 的属性窗口
JRExporter htmlExporter = new JRHtmlExporter();
ByteArrayOutputStream htmlOut = new ByteArrayOutputStream();
htmlExporter.setParameter(JRHtmlExporterParameter.JASPER_PRINT,
jasperPrint);
htmlExporter.setParameter(JRHtmlExporterParameter.OUTPUT_STREAM,
htmlOut);
htmlExporter.setParameter(JRHtmlExporterParameter.CHARACTER_ENCODING,
"utf-8");
htmlExporter
.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,
Boolean.FALSE);
htmlExporter.exportReport();
File outFile = new File( "F:/测试/report2.html");
FileOutputStream os = new FileOutputStream(outFile);
os.write(htmlOut.toByteArray());
os.close();
htmlOut.close();
}
public void showReportApplet() throws Exception {
Map<String, Object> param = new HashMap<String, Object>();
param.put("REPORT_CONNECTION", getConn());
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperPath,
param);
JasperViewer.viewReport(jasperPrint, false);
}
public Connection getConn() {
String driverClass = "com.mysql.jdbc.Driver";
String conStr = "jdbc:mysql://127.0.0.1/tt";
String user = "root";
String password = "123";
Connection conn = null;
try {
Class.forName(driverClass);
conn = (Connection) DriverManager.getConnection(conStr, user,
password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
}
遇到的问题:poi导致错误的忘记了,O(∩_∩)O哈哈~
Exception in thread "main" java.lang.NoClassDefFoundError: com/lowagie/text/DocumentException
at net.sf.jasperreports.engine.JasperExportManager.exportReportToPdfFile(JasperExportManager.java:121)
at jasperreportsdemoapp.Main.main(Main.java:67)
..............
这个就是开始说的itext包不正确导致的参考下面的网站解决掉了,呵呵
参考:http://blogs.sun.com/geertjan/entry/itext_and_jasper_using_them
其中注意一下提示的错误NoClassDefFoundError,并非是说你没有添加相关的jar包到classpath,而极可能是版本不对,参阅下面:
Difference between ClassNotFoundException and NoClassDefFoundError
http://www.jroller.com/sjivan/entry/difference_between_classnotfoundexception_and_noclassdeffounderror
最后提一下,jasper现在使用需要辅助jar包的情况:
JasperReports Requirements
http://jasperforge.org/website/jasperreportswebsite/trunk/documentation.html?header=project&target=jasperreports
[color=red]w[b]eb中应用jasperreport[/b][/color]:
[url]http://923080512.iteye.com/blog/1396484[/url]
在web中使用jasperreport 要导入的包:
jasperreports-4.5.0.jar
js_activation-1.1.jar
iText-2.1.7.jar
iTextAsian.jar
groovy-all-1.7.5.jar
commons-beanutils-1.8.2.jar
commons-digester-1.7.jar
commons-collections-3.2.1.jar
poi-3.7-20101029.jar
方法1:
String filename = ServletActionContext.getServletContext().
getRealPath("/ireport/report5_Javabean.jasper");
Map<String,Object> map = new HashMap<String,Object>();
map.put("SQLSTR", "select * from syz");
Collection<Syz> collection = service.findAll();
JRDataSource datasource = new JRBeanCollectionDataSource(collection);
ServletOutputStream out = response.getOutputStream();
//加载jasper文件,生成JasperReport实例 ;
JasperReport report = (JasperReport) JRLoader.loadObject(filename);
//填充报表,生成JasperPrint实例
JasperPrint print = JasperFillManager.fillReport(report,map,datasource);
//JRXlsExporter是excel导出器,JRPdfExporter 是pdf导出器,其他类型以此类推
JRAbstractExporter exporter = new JRXlsExporter();
//导出器设置参数
exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
//导出
exporter.exportReport();
方法2:
byte[] b = null ;
response.setContentType("application/pdf");
response.setHeader("Content-Disposition","filename="+
java.net.URLEncoder.encode("Syz_PDF"+new SimpleDateFormat("yyyyMMddHHmmssSS").format(new Date()), "UTF-8")
+ ".PDF");
//转换成pdf,如果是html,转换后直接response.sendRedirect()
b = JasperRunManager.runReportToPdf(filename, map, datasource);
out.write(b);
out.flush();
out.close();