iReport多张报表连续输出,
java代码部分怎么写?
我在网上找了一段这样的代码,但是出不来啊,谁能帮我解决下或者有完整的一个例子给我看看,谢谢!
ServletContext servletContext =this.getServletContext();
   JasperPrint jasperPrint = JasperFillManager.fillReport(servletContext.getRealPath("/Jasper/test_report_3.jasper"), parameters, dataSource);
   JasperPrint jasperPrint2 = JasperFillManager.fillReport(servletContext.getRealPath("/Jasper/test_report_2.jasper"), parameters, conn);
   List jasperPrintList = new ArrayList();
   jasperPrintList.add(jasperPrint);
   jasperPrintList.add(jasperPrint2);
  
   //获取输出字节流
   ByteArrayOutputStream baos = new ByteArrayOutputStream();
   JRPdfExporter exporter = new JRPdfExporter();
   exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST,jasperPrintList);
   exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
   exporter.exportReport();
   byte[] bytes= baos.toByteArray();

解决方案 »

  1.   

    UTIL类:需要导入iTextAsian.jar类库
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.OutputStream;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    import java.util.Random;import net.sf.jasperreports.engine.JRDataSource;
    import net.sf.jasperreports.engine.JRExporterParameter;
    import net.sf.jasperreports.engine.JasperFillManager;
    import net.sf.jasperreports.engine.JasperPrint;
    import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
    import net.sf.jasperreports.engine.export.JRPdfExporter;public class JasperReportUtils { public static void main(String[] args) {
    List dataList = new ArrayList();
    List test0 = new ArrayList();
    List test1 = new ArrayList();
    String names[] = { "周", "吴", "郑", "王" };
    String givens[] = { "一", "二", "三", "四", "五", "六", "七", "八", "九" };
    Random random = new Random(10);
    for (int i = 0; i < 100; i++) {
    TestBean0 bean0 = new TestBean0();
    TestBean1 bean1 = new TestBean1();
    bean0.setId(i);
    bean1.setId(i);
    bean0.setName("name0-" + i);
    bean1.setName("name1-" + i);
    bean1.setFamilyName(names[i % 4] + i);
    bean1.setGivenName(givens[random.nextInt(9)] + givens[random.nextInt(9)]);
    test0.add(bean0);
    test1.add(bean1);
    }
    dataList.add(test0);
    dataList.add(test1);
    List jspFiles = new ArrayList();
    jspFiles.add("D:/muiltiexp0.jasper");
    jspFiles.add("D:/muiltiexp1.jasper");
    File file = new File("D:/muiltiexp.pdf");
    try {
    JasperReportUtils.reportPdf(jspFiles, null, dataList, new FileOutputStream(file));
    } catch (Exception e) {
    e.printStackTrace();
    } finally { }
    } public static void reportPdf(List<String> templates, List<Map> reportParams, List<List> reportDataList,
    OutputStream out) {
    List<JasperPrint> jasperPrintList = new ArrayList<JasperPrint>();
    for (int i = 0; i < reportDataList.size(); i++) {
    try {
    String source = templates.get(i);
    List<?> reportData = reportDataList.get(i);
    Map<?, ?> param = null;
    if (reportParams != null && reportParams.size() >= i) {
    param = reportParams.get(i);
    }
    JRDataSource dataSource = new JRBeanCollectionDataSource(reportData);
    JasperPrint jasperPrint = JasperFillManager.fillReport(source, param, dataSource);
    jasperPrintList.add(jasperPrint);
    } catch (Exception e) {
    e.printStackTrace();
    continue;
    }
    }
    try {
    JRPdfExporter exporter = new JRPdfExporter();
    exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);
    exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
    exporter.exportReport();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
    BEAN类
    public class TestBean0 {
    private int id;
    private String name; public int getId() {
    return id;
    } public String getName() {
    return name;
    } public void setId(int id) {
    this.id = id;
    } public void setName(String name) {
    this.name = name;
    }
    }
    ----
    public class TestBean1 extends TestBean0 {
    private String familyName;
    private String givenName; public String getFamilyName() {
    return familyName;
    } public String getGivenName() {
    return givenName;
    } public void setFamilyName(String familyName0) {
    this.familyName = familyName0;
    } public void setGivenName(String givenName0) {
    this.givenName = givenName0;
    }
    }
    ========
      

  2.   

    2个jrxml文件:
    0:
    <?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="muiltiexp0" pageWidth="595" pageHeight="842" columnWidth="595" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0">
    <property name="ireport.scriptlethandling" value="0"/>
    <property name="ireport.encoding" value="UTF-8"/>
    <import value="net.sf.jasperreports.engine.*"/>
    <import value="java.util.*"/>
    <import value="net.sf.jasperreports.engine.data.*"/>
    <reportFont name="inventory" isDefault="true" fontName="宋体" size="8" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
    <style name="inventory" isDefault="true" hAlign="Center" vAlign="Middle" up="styled" fontName="宋体" fontSize="8" isBold="false" isItalic="false" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="false"/>
    <field name="id" class="java.lang.Integer"/>
    <field name="name" class="java.lang.String"/>
    <background>
    <band splitType="Stretch"/>
    </background>
    <title>
    <band height="35" splitType="Stretch">
    <staticText>
    <reportElement x="253" y="7" width="100" height="20"/>
    <textElement>
    <font fontName="sansserif" size="12"/>
    </textElement>
    <text><![CDATA[多表单导出测试0]]></text>
    </staticText>
    </band>
    </title>
    <pageHeader>
    <band height="29" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
    <band height="24" splitType="Stretch">
    <staticText>
    <reportElement key="staticText-4" style="inventory" x="247" y="2" width="53" height="21"/>
    <textElement>
    <font reportFont="inventory"/>
    </textElement>
    <text><![CDATA[ID]]></text>
    </staticText>
    <staticText>
    <reportElement key="staticText-5" style="inventory" x="301" y="2" width="53" height="21"/>
    <textElement>
    <font reportFont="inventory" fontName="sansserif" size="9" isStrikeThrough="false" pdfFontName="STSong-Light" isPdfEmbedded="false"/>
    </textElement>
    <text><![CDATA[用户名]]></text>
    </staticText>
    <line>
    <reportElement key="line-1" style="inventory" x="247" y="0" width="108" height="2"/>
    <graphicElement>
    <pen lineWidth="0.95" lineStyle="Solid"/>
    </graphicElement>
    </line>
    <line direction="BottomUp">
    <reportElement key="line-2" style="inventory" x="247" y="2" width="1" height="21"/>
    <graphicElement>
    <pen lineWidth="0.95" lineStyle="Solid"/>
    </graphicElement>
    </line>
    <line direction="BottomUp">
    <reportElement key="line-5" style="inventory" x="354" y="2" width="1" height="21"/>
    <graphicElement>
    <pen lineWidth="0.95" lineStyle="Solid"/>
    </graphicElement>
    </line>
    <line direction="BottomUp">
    <reportElement key="line-6" style="inventory" x="300" y="2" width="1" height="21"/>
    <graphicElement>
    <pen lineWidth="0.95" lineStyle="Solid"/>
    </graphicElement>
    </line>
    <line>
    <reportElement key="line-7" style="inventory" x="247" y="23" width="108" height="1"/>
    <graphicElement>
    <pen lineWidth="0.95" lineStyle="Solid"/>
    </graphicElement>
    </line>
    </band>
    </columnHeader>
    <detail>
    <band height="22" splitType="Stretch">
    <textField isBlankWhenNull="false">
    <reportElement key="textField-3" x="247" y="1" width="52" height="21"/>
    <textElement textAlignment="Center" verticalAlignment="Middle">
    <font reportFont="inventory"/>
    </textElement>
    <textFieldExpression class="java.lang.Integer"><![CDATA[$F{id}]]></textFieldExpression>
    </textField>
    <textField isBlankWhenNull="false">
    <reportElement key="textField-4" x="301" y="1" width="52" height="21"/>
    <textElement textAlignment="Center" verticalAlignment="Middle">
    <font reportFont="inventory"/>
    </textElement>
    <textFieldExpression class="java.lang.String"><![CDATA[$F{name}]]></textFieldExpression>
    </textField>
    <line direction="BottomUp">
    <reportElement key="line-8" style="inventory" x="247" y="0" width="1" height="21"/>
    <graphicElement>
    <pen lineWidth="0.95" lineStyle="Solid"/>
    </graphicElement>
    </line>
    <line direction="BottomUp">
    <reportElement key="line-9" style="inventory" x="300" y="0" width="1" height="21"/>
    <graphicElement>
    <pen lineWidth="0.95" lineStyle="Solid"/>
    </graphicElement>
    </line>
    <line direction="BottomUp">
    <reportElement key="line-10" style="inventory" x="354" y="0" width="1" height="21"/>
    <graphicElement>
    <pen lineWidth="0.95" lineStyle="Solid"/>
    </graphicElement>
    </line>
    <line>
    <reportElement key="line-14" style="inventory" x="247" y="21" width="108" height="1"/>
    <graphicElement>
    <pen lineWidth="0.95" lineStyle="Solid"/>
    </graphicElement>
    </line>
    </band>
    </detail>
    <columnFooter>
    <band splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
    <band height="40" splitType="Stretch">
    <textField isBlankWhenNull="false">
    <reportElement key="textField-11" x="446" y="22" width="73" height="18"/>
    <textElement textAlignment="Right" verticalAlignment="Middle">
    <font reportFont="inventory"/>
    </textElement>
    <textFieldExpression class="java.lang.String"><![CDATA["第" + $V{PAGE_NUMBER} + "页 共"]]></textFieldExpression>
    </textField>
    <textField evaluationTime="Report" isBlankWhenNull="false">
    <reportElement key="textField-12" x="519" y="22" width="17" height="18"/>
    <textElement verticalAlignment="Middle">
    <font reportFont="inventory"/>
    </textElement>
    <textFieldExpression class="java.lang.String"><![CDATA["" + $V{PAGE_NUMBER} + "页"]]></textFieldExpression>
    </textField>
    </band>
    </pageFooter>
    <summary>
    <band height="23" splitType="Stretch"/>
    </summary>
    </jasperReport>
    --------
      

  3.   

    1:
    <?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="muiltiexp1" pageWidth="595" pageHeight="842" columnWidth="595" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0">
    <property name="ireport.scriptlethandling" value="0"/>
    <property name="ireport.encoding" value="UTF-8"/>
    <import value="net.sf.jasperreports.engine.*"/>
    <import value="java.util.*"/>
    <import value="net.sf.jasperreports.engine.data.*"/>
    <reportFont name="inventory" isDefault="true" fontName="宋体" size="8" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="true"/>
    <style name="inventory" isDefault="true" hAlign="Center" vAlign="Middle" up="styled" fontName="宋体" fontSize="8" isBold="false" isItalic="false" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="false"/>
    <field name="id" class="java.lang.Integer"/>
    <field name="name" class="java.lang.String"/>
    <field name="familyName" class="java.lang.String"/>
    <field name="givenName" class="java.lang.String"/>
    <background>
    <band splitType="Stretch"/>
    </background>
    <title>
    <band height="35" splitType="Stretch">
    <staticText>
    <reportElement x="252" y="6" width="100" height="20"/>
    <textElement>
    <font fontName="sansserif" size="12"/>
    </textElement>
    <text><![CDATA[多表单导出测试1]]></text>
    </staticText>
    </band>
    </title>
    <pageHeader>
    <band height="29" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
    <band height="24" splitType="Stretch">
    <staticText>
    <reportElement key="staticText-4" style="inventory" x="191" y="2" width="53" height="21"/>
    <textElement>
    <font reportFont="inventory"/>
    </textElement>
    <text><![CDATA[ID]]></text>
    </staticText>
    <staticText>
    <reportElement key="staticText-5" style="inventory" x="245" y="2" width="53" height="21"/>
    <textElement>
    <font reportFont="inventory" fontName="sansserif" size="9" isStrikeThrough="false" pdfFontName="STSong-Light" isPdfEmbedded="false"/>
    </textElement>
    <text><![CDATA[用户名]]></text>
    </staticText>
    <staticText>
    <reportElement key="staticText-6" style="inventory" x="299" y="2" width="53" height="21"/>
    <textElement>
    <font reportFont="inventory" fontName="sansserif" size="9"/>
    </textElement>
    <text><![CDATA[姓]]></text>
    </staticText>
    <staticText>
    <reportElement key="staticText-7" style="inventory" x="353" y="2" width="53" height="21"/>
    <textElement>
    <font reportFont="inventory" fontName="sansserif" size="9" pdfFontName="STSong-Light" isPdfEmbedded="false"/>
    </textElement>
    <text><![CDATA[名]]></text>
    </staticText>
    <line>
    <reportElement key="line-1" style="inventory" x="191" y="1" width="216" height="1"/>
    <graphicElement>
    <pen lineWidth="0.95" lineStyle="Solid"/>
    </graphicElement>
    </line>
    <line direction="BottomUp">
    <reportElement key="line-2" style="inventory" x="191" y="2" width="1" height="21"/>
    <graphicElement>
    <pen lineWidth="0.95" lineStyle="Solid"/>
    </graphicElement>
    </line>
    <line direction="BottomUp">
    <reportElement key="line-3" style="inventory" x="352" y="2" width="1" height="21"/>
    <graphicElement>
    <pen lineWidth="0.95" lineStyle="Solid"/>
    </graphicElement>
    </line>
    <line direction="BottomUp">
    <reportElement key="line-4" style="inventory" x="406" y="2" width="1" height="21"/>
    <graphicElement>
    <pen lineWidth="0.95" lineStyle="Solid"/>
    </graphicElement>
    </line>
    <line direction="BottomUp">
    <reportElement key="line-5" style="inventory" x="298" y="2" width="1" height="21"/>
    <graphicElement>
    <pen lineWidth="0.95" lineStyle="Solid"/>
    </graphicElement>
    </line>
    <line direction="BottomUp">
    <reportElement key="line-6" style="inventory" x="244" y="2" width="1" height="21"/>
    <graphicElement>
    <pen lineWidth="0.95" lineStyle="Solid"/>
    </graphicElement>
    </line>
    <line>
    <reportElement key="line-7" style="inventory" x="191" y="23" width="216" height="1"/>
    <graphicElement>
    <pen lineWidth="0.95" lineStyle="Solid"/>
    </graphicElement>
    </line>
    </band>
    </columnHeader>
    <detail>
    <band height="22" splitType="Stretch">
    <textField isBlankWhenNull="false">
    <reportElement key="textField-3" x="191" y="1" width="52" height="21"/>
    <textElement textAlignment="Center" verticalAlignment="Middle">
    <font reportFont="inventory"/>
    </textElement>
    <textFieldExpression class="java.lang.Integer"><![CDATA[$F{id}]]></textFieldExpression>
    </textField>
    <textField isBlankWhenNull="false">
    <reportElement key="textField-4" x="245" y="1" width="52" height="21"/>
    <textElement textAlignment="Center" verticalAlignment="Middle">
    <font reportFont="inventory"/>
    </textElement>
    <textFieldExpression class="java.lang.String"><![CDATA[$F{name}]]></textFieldExpression>
    </textField>
    <textField isBlankWhenNull="false">
    <reportElement key="textField-5" x="299" y="1" width="52" height="21"/>
    <textElement textAlignment="Center">
    <font reportFont="inventory"/>
    </textElement>
    <textFieldExpression class="java.lang.String"><![CDATA[$F{familyName}]]></textFieldExpression>
    </textField>
    <textField isBlankWhenNull="false">
    <reportElement key="textField-6" x="353" y="1" width="52" height="21"/>
    <textElement textAlignment="Center">
    <font reportFont="inventory"/>
    </textElement>
    <textFieldExpression class="java.lang.String"><![CDATA[$F{givenName}]]></textFieldExpression>
    </textField>
    <line direction="BottomUp">
    <reportElement key="line-8" style="inventory" x="191" y="0" width="1" height="21"/>
    <graphicElement>
    <pen lineWidth="0.95" lineStyle="Solid"/>
    </graphicElement>
    </line>
    <line direction="BottomUp">
    <reportElement key="line-9" style="inventory" x="244" y="0" width="1" height="21"/>
    <graphicElement>
    <pen lineWidth="0.95" lineStyle="Solid"/>
    </graphicElement>
    </line>
    <line direction="BottomUp">
    <reportElement key="line-10" style="inventory" x="298" y="0" width="1" height="21"/>
    <graphicElement>
    <pen lineWidth="0.95" lineStyle="Solid"/>
    </graphicElement>
    </line>
    <line direction="BottomUp">
    <reportElement key="line-11" style="inventory" x="352" y="0" width="1" height="21"/>
    <graphicElement>
    <pen lineWidth="0.95" lineStyle="Solid"/>
    </graphicElement>
    </line>
    <line direction="BottomUp">
    <reportElement key="line-12" style="inventory" x="406" y="0" width="1" height="21"/>
    <graphicElement>
    <pen lineWidth="0.95" lineStyle="Solid"/>
    </graphicElement>
    </line>
    <line>
    <reportElement key="line-14" style="inventory" x="191" y="21" width="214" height="1"/>
    <graphicElement>
    <pen lineWidth="0.95" lineStyle="Solid"/>
    </graphicElement>
    </line>
    </band>
    </detail>
    <columnFooter>
    <band splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
    <band height="40" splitType="Stretch">
    <textField isBlankWhenNull="false">
    <reportElement key="textField-11" x="446" y="22" width="73" height="18"/>
    <textElement textAlignment="Right" verticalAlignment="Middle">
    <font reportFont="inventory"/>
    </textElement>
    <textFieldExpression class="java.lang.String"><![CDATA["第" + $V{PAGE_NUMBER} + "页 共"]]></textFieldExpression>
    </textField>
    <textField evaluationTime="Report" isBlankWhenNull="false">
    <reportElement key="textField-12" x="519" y="22" width="17" height="18"/>
    <textElement verticalAlignment="Middle">
    <font reportFont="inventory"/>
    </textElement>
    <textFieldExpression class="java.lang.String"><![CDATA["" + $V{PAGE_NUMBER} + "页"]]></textFieldExpression>
    </textField>
    </band>
    </pageFooter>
    <summary>
    <band height="23" splitType="Stretch"/>
    </summary>
    </jasperReport>