本文只涉及到简单的动态数据传递
首先使用ireport创建模板
关于 变量 $p{}格式的,是parameter类型的,可以通过Map传递给JasperReport
Field 可以设置一些可以动态从数据库获取的数据,但是这种类型需要传递给一JasperReport一个Connection类型的对象
关于JasperReport的几种文件格式 jrxml是资源文件 其实就是一个xml文件,使用ireport编辑的就是这种文件
jasperReport可以根据jrxml文件编译生成jasper文件,应该就是一种字节码文件
jasper文件就是一种模板,是没有数据的,把数据交给jasper文件之后还可以生成一种jprint文件
通过jprint可以利用JasperReport生成各种格式的输出,如pdf excel html xml
代码:
纯静态的一个:
public static void main(String[] args) throws Exception{ String filename = "src/jasper/report/test/hello.jrxml"; long startTime = System.currentTimeMillis(); //编译源文件 String jasperfile = JasperCompileManager.compileReportToFile(filename); //填充数据,这里填充的是空数据 String jrprintfile = JasperFillManager.fillReportToFile(jasperfile,null,new JREmptyDataSource()); //输出html文件 JasperExportManager.exportReportToHtmlFile(jrprintfile); //输入pdf文件,需要itext类库 JasperExportManager.exportReportToPdfFile(jrprintfile); //输出xml文件,扩展名是jrpxml JasperExportManager.exportReportToXmlFile(jrprintfile,false); //下面的代码是输出excel文件的 File sourceFile = new File(jrprintfile); System.out.println(sourceFile.getAbsolutePath()); JasperPrint printer = (JasperPrint) JRLoader.loadObject(sourceFile); File destFile = new File(sourceFile.getParent(),printer.getName()+".xls"); JRXlsExporter exporter = new JRXlsExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, printer); exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,destFile.toString()); exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,true); exporter.exportReport(); long endTime = System.currentTimeMillis(); long time = (endTime-startTime); System.out.println("done in " + time + " ms ..."); }
动态传递数据的:
jrxml文件 2002.jrxml
<?xml version="1.0" encoding="UTF-8"?> <jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="2002" pageWidth="835" pageHeight="842" columnWidth="795" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20"> <parameter name="startTime" class="java.lang.String"/> <parameter name="endTime" class="java.lang.String"/> <parameter name="titleName" class="java.lang.String"/> <background> <band/> </background> <title> <band height="476"> <staticText> <reportElement x="568" y="33" width="75" height="20"/> <textElement textAlignment="Center" verticalAlignment="Middle"> <font fontName="宋体" size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H"/> </textElement> <text><![CDATA[结束时间:]]></text> </staticText> <textField isBlankWhenNull="true"> <reportElement x="643" y="33" width="150" height="20"/> <textElement textAlignment="Center" verticalAlignment="Middle"> <font fontName="宋体" size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H"/> </textElement> <textFieldExpression class="java.lang.String"><![CDATA[$P{endTime}]]></textFieldExpression> </textField> <staticText> <reportElement x="0" y="33" width="75" height="20"/> <textElement textAlignment="Center" verticalAlignment="Middle"> <font fontName="宋体" size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H"/> </textElement> <text><![CDATA[开始时间:]]></text> </staticText> <textField isBlankWhenNull="true"> <reportElement x="75" y="33" width="150" height="20"/> <textElement textAlignment="Center" verticalAlignment="Middle"> <font fontName="宋体" size="12" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H"/> </textElement> <textFieldExpression class="java.lang.String"><![CDATA[$P{startTime}]]></textFieldExpression> </textField> <line> <reportElement x="140" y="59" width="653" height="1"/> </line> <staticText> <reportElement x="40" y="53" width="100" height="20"/> <textElement textAlignment="Center" verticalAlignment="Middle"> <font fontName="宋体" size="12" isBold="true" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H"/> </textElement> <text><![CDATA[图表]]></text> </staticText> <line> <reportElement x="0" y="60" width="40" height="1"/> </line> <textField isStretchWithOverflow="true" isBlankWhenNull="true"> <reportElement x="0" y="0" width="795" height="33"/> <textElement textAlignment="Center" verticalAlignment="Middle"> <font fontName="宋体" size="14" isBold="true" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H"/> </textElement> <textFieldExpression class="java.lang.String"><![CDATA[$P{titleName}]]></textFieldExpression> </textField> </band> </title> <pageHeader> <band/> </pageHeader> <columnHeader> <band/> </columnHeader> <detail> <band/> </detail> <columnFooter> <band/> </columnFooter> <pageFooter> <band/> </pageFooter> <summary> <band/> </summary> </jasperReport>
Java程序:
package jasper.report.test; import java.io.File; import java.util.Date; import java.util.HashMap; import java.util.Map; import net.sf.jasperreports.engine.JRDataSource; import net.sf.jasperreports.engine.JREmptyDataSource; import net.sf.jasperreports.engine.JRExporterParameter; import net.sf.jasperreports.engine.JasperCompileManager; import net.sf.jasperreports.engine.JasperExportManager; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.export.JRXlsExporter; import net.sf.jasperreports.engine.export.JRXlsExporterParameter; import net.sf.jasperreports.engine.util.JRLoader; public class TestUseDynamicParameter { public static void main(String[] args) throws Exception{ final String conf = "src/jasper/report/test/2002.jrxml"; /////////////////////////////////////// Map<String,String> map = new HashMap<String,String>(); map.put("endTime", new Date().toString()); map.put("startTime", new Date().toString()); map.put("titleName", "thisisthetitlename"); /////////////////////////////////////// long startTime = System.currentTimeMillis(); //编译源文件 String jasperfile = JasperCompileManager.compileReportToFile(conf); //填充数据,这里填充的是空数据 String jrprintfile = JasperFillManager.fillReportToFile(jasperfile, map,new JREmptyDataSource()); //输出html文件 JasperExportManager.exportReportToHtmlFile(jrprintfile); //输入pdf文件,需要itext类库 JasperExportManager.exportReportToPdfFile(jrprintfile); //输出xml文件,扩展名是jrpxml JasperExportManager.exportReportToXmlFile(jrprintfile,false); // //下面的代码是输出excel文件的 File sourceFile = new File(jrprintfile); System.out.println(sourceFile.getAbsolutePath()); JasperPrint printer = (JasperPrint) JRLoader.loadObject(sourceFile); File destFile = new File(sourceFile.getParent(),printer.getName()+".xls"); JRXlsExporter exporter = new JRXlsExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, printer); exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,destFile.toString()); exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,true); exporter.exportReport(); long endTime = System.currentTimeMillis(); long time = (endTime-startTime); System.out.println("done in " + time + " ms ..."); } }