使用Jasperreporter生成入库出库单打印等报表操作

项目需要打印报表:就是那种生成入库单,出库单等的操作。使用到的技术:使用iReport Designer5.1.0设计报表,使用struts2+jasperreporter生成最终填充数据的报表

首先看一下效果图

一、使用iReport Designer 设计报表

  1、安装iReport Designer,过程比较简单。需要强调一下,5.1.0版本不支持jdk8,我又重新装了jdk7.然后在iReport-5.1.0\etc\ireport.conf下配置一下jdkhome,如下图所示为我的配置截图

   2、打开iReport后,可以设置一下存储文件的位置,在工具->选项->Compilation and execution 中配置Compilation directory和Virtualizer中的Directory where the paged out data is to be stored为自定义路径。

  3、创建一个新的空白报表,并添加相关元素。如下右侧为组件面板,通过其中的Static Text,Text Field,Line等设计出简单的报表

如下所示为xml源码:

<?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="OutputReport" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="5d594fbe-e7f6-4ee9-a586-8394b1a91bef">
    <property name="ireport.zoom" value="1.6500000000000001"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <parameter name="id" class="java.lang.String"/>
    <parameter name="outputtime" class="java.util.Date"/>
    <parameter name="operator" class="java.lang.String"/>
    <field name="materialid" class="java.lang.String"/>
    <field name="materialname" class="java.lang.String"/>
    <field name="assetno" class="java.lang.String"/>
    <field name="category" class="java.lang.String"/>
    <field name="model" class="java.lang.String"/>
    <field name="specification" class="java.lang.String"/>
    <field name="unit" class="java.lang.String"/>
    <field name="amount" class="java.lang.String"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="59" splitType="Stretch">
            <staticText>
                <reportElement uuid="9e2e3b53-c9bc-49aa-a4a0-cbcc212b1290" x="193" y="4" width="142" height="32" forecolor="#CC3300"/>
                <textElement textAlignment="Center" verticalAlignment="Middle" rotation="None" markup="none">
                    <font fontName="SansSerif" size="18" isBold="true" isItalic="false" isUnderline="false" isStrikeThrough="false"/>
                </textElement>
                <text><![CDATA[XXX单位出库单]]></text>
            </staticText>
            <line>
                <reportElement uuid="954d8309-d574-469f-8d60-27520f6067ed" x="0" y="46" width="555" height="1"/>
                <graphicElement>
                    <pen lineWidth="5.0"/>
                </graphicElement>
            </line>
        </band>
    </title>
    <pageHeader>
        <band height="31" splitType="Stretch">
            <textField isBlankWhenNull="true">
                <reportElement uuid="1f85a2a7-7cde-4e3a-b485-78700edbc1bb" x="86" y="0" width="130" height="20"/>
                <textElement textAlignment="Left" verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$P{id}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement uuid="43abcb33-52d3-4ecc-ae8b-1458a5577f59" x="216" y="0" width="64" height="20"/>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <text><![CDATA[出库时间:]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="155052e3-f990-409b-86d1-eaeb1ae65cf1" x="391" y="0" width="64" height="20"/>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <text><![CDATA[操作人员:]]></text>
            </staticText>
            <textField isBlankWhenNull="true">
                <reportElement uuid="1f2ef840-d135-438d-9975-51e6f2856751" x="455" y="0" width="100" height="20"/>
                <textElement verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$P{operator}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement uuid="cf5353cd-1f0d-4d53-bb56-89c1842f26f4" x="0" y="0" width="86" height="20"/>
                <textElement textAlignment="Center" verticalAlignment="Middle"/>
                <text><![CDATA[出库单号:]]></text>
            </staticText>
            <textField isBlankWhenNull="true">
                <reportElement uuid="e22b2fbe-dc33-4f6c-b302-97d0d34d21c9" x="280" y="0" width="111" height="20"/>
                <textElement verticalAlignment="Middle"/>
                <textFieldExpression><![CDATA[$P{outputtime}]]></textFieldExpression>
            </textField>
            <line>
                <reportElement uuid="28bfcbc5-d841-4cd1-825a-b182740f8785" x="0" y="27" width="555" height="1"/>
            </line>
        </band>
    </pageHeader>
    <columnHeader>
        <band height="61" splitType="Stretch">
            <staticText>
                <reportElement uuid="7081f40d-6df7-475c-ba7d-b5edb2d8f0b1" mode="Transparent" x="165" y="7" width="76" height="44" forecolor="#000000" backcolor="#CFFFCC"/>
                <textElement verticalAlignment="Middle"/>
                <text><![CDATA[资产编号]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="fdd65f3d-3b51-4b5b-8153-e085a30237d8" mode="Transparent" x="471" y="7" width="62" height="44" forecolor="#000000" backcolor="#CFFFCC"/>
                <textElement verticalAlignment="Middle"/>
                <text><![CDATA[出库数量]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="a0568192-b022-4d4a-aa74-7930e6fac339" mode="Transparent" x="2" y="7" width="98" height="44" forecolor="#000000" backcolor="#CFFFCC"/>
                <textElement verticalAlignment="Middle"/>
                <text><![CDATA[物资编号]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="620a6747-442b-42ce-9765-50240120458f" mode="Transparent" x="100" y="7" width="65" height="44" forecolor="#000000" backcolor="#CFFFCC"/>
                <textElement verticalAlignment="Middle"/>
                <text><![CDATA[物资名称]]></text>
            </staticText>
            <line>
                <reportElement uuid="edd268c2-e05a-4afb-a08d-deaefbabd499" x="0" y="55" width="555" height="1"/>
                <graphicElement>
                    <pen lineWidth="2.0" lineStyle="Double"/>
                </graphicElement>
            </line>
            <line>
                <reportElement uuid="c29f438c-54dd-448d-a49f-aadede5719ee" x="0" y="3" width="555" height="1"/>
                <graphicElement>
                    <pen lineWidth="2.0" lineStyle="Double"/>
                </graphicElement>
            </line>
            <staticText>
                <reportElement uuid="995f7687-471d-413e-9400-4335f54c4bfd" mode="Transparent" x="241" y="7" width="76" height="44" forecolor="#000000" backcolor="#CFFFCC"/>
                <textElement verticalAlignment="Middle"/>
                <text><![CDATA[物资类型]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="a90e0bb0-9b7c-4a94-b6cc-0a3d24c79e59" mode="Transparent" x="317" y="7" width="45" height="44" forecolor="#000000" backcolor="#CFFFCC"/>
                <textElement verticalAlignment="Middle"/>
                <text><![CDATA[型号]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="189b7d59-df3a-4f07-a3d3-c2da2ca478e9" mode="Transparent" x="362" y="7" width="47" height="44" forecolor="#000000" backcolor="#CFFFCC"/>
                <textElement verticalAlignment="Middle"/>
                <text><![CDATA[规格]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="3f3c3bd0-81d5-4e97-a088-5396605f62c4" mode="Transparent" x="409" y="7" width="62" height="44" forecolor="#000000" backcolor="#CFFFCC"/>
                <textElement verticalAlignment="Middle"/>
                <text><![CDATA[单位]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="35" splitType="Stretch">
            <textField isBlankWhenNull="true">
                <reportElement uuid="6bbf81e8-5f85-4f01-839b-2fdeeae82fb8" x="0" y="4" width="98" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{materialid}]]></textFieldExpression>
            </textField>
            <textField isBlankWhenNull="true">
                <reportElement uuid="56fcdc7d-8baa-486c-9c44-e9a6c5c47be3" x="100" y="4" width="65" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{materialname}]]></textFieldExpression>
            </textField>
            <textField isBlankWhenNull="true">
                <reportElement uuid="778d0943-5fc2-402e-8a57-f1ff92d1d2b0" x="165" y="4" width="76" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{assetno}]]></textFieldExpression>
            </textField>
            <textField isBlankWhenNull="true">
                <reportElement uuid="f2b8e55c-ecf8-4477-a361-14fcd03ca704" x="241" y="4" width="76" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{category}]]></textFieldExpression>
            </textField>
            <textField isBlankWhenNull="true">
                <reportElement uuid="08d6ec09-9bf7-4eac-aa1e-f0ac71b5ec8b" x="317" y="4" width="45" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{model}]]></textFieldExpression>
            </textField>
            <textField isBlankWhenNull="true">
                <reportElement uuid="83613e2b-2cd6-4e3e-acfd-dee02601c3f0" x="362" y="4" width="47" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{specification}]]></textFieldExpression>
            </textField>
            <textField isBlankWhenNull="true">
                <reportElement uuid="0cba1ef6-27d7-4e78-a6a7-6cc9bf6f6e39" x="409" y="4" width="62" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{unit}]]></textFieldExpression>
            </textField>
            <textField isBlankWhenNull="true">
                <reportElement uuid="b23218d6-7bce-4208-bffc-f6743481b9e9" x="471" y="4" width="62" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{amount}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <columnFooter>
        <band height="30" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="27" splitType="Stretch">
            <staticText>
                <reportElement uuid="710ece90-d059-493b-ae83-b0d6d9d036cf" x="391" y="3" width="64" height="20"/>
                <textElement/>
                <text><![CDATA[生成时间:]]></text>
            </staticText>
            <textField pattern="EEEEE dd MMMMM yyyy">
                <reportElement uuid="992f84e2-197f-4064-ade0-78c22f75dcbc" x="455" y="3" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[new java.util.Date()]]></textFieldExpression>
            </textField>
        </band>
    </pageFooter>
    <summary>
        <band height="42" splitType="Stretch"/>
    </summary>
</jasperReport>

 

  4、编译文件为japer格式的文件,用于程序中使用:右键项目Compile Report 生成相应的文件,如下所示。

二、在Struts2中使用上述的jasper文件填充数据生成报表

  1、需要在项目中添加jar包,如下所示

  2、编写action文件,我在项目中使用全注解方式配置jasperreporter。重点是(1)、ParentPackage为jasperreports-default。(2)、我使用从程序中获取数据然后填充到jasper文件而不是直接绑定到数据库数据的。所以定义了如下的list和params,分别用于填充$F{}和$P{}。

List list;
HashMap params;

(3)、最重要的是在方法上的@Aciton配置
 @Action(value = "outputOrder", results = { @Result(name = "outputOrder", type = "jasper", params = {
            "location", "/jasper/OutputReport.jasper", "format", "HTML",
            "dataSource", "list", "reportParameters", "params",
            "imageServletUrl", "/servlets/image?image=", "contentDisposition",
            "inline" }) })
其中type为jasper,定义的params中,location表示使用的jasper文件的位置,format表示输出文件的格式为HTML,datasource表示填充的循环的数据$F{...},reportParmeters为填充数据$P{...}。最后的imageServletUrl
是用来填充网页中的空白的,否则会出现好多的叉叉。 具体需要在web.xml中配置如下的servlet,这是struts2-jasperreports-plugin-2.3.16.1.jar中自带的。
<servlet>
        <servlet-name>ImageServlet</servlet-name>
        <servlet-class>net.sf.jasperreports.j2ee.servlets.ImageServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ImageServlet</servlet-name>
        <url-pattern>/servlets/image</url-pattern>
    </servlet-mapping>

 

最后附上struts2的action实现源代码如下:

  

package action.japser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;

import annotation.SysControllerLogAnnotation;
import entity.InputDetails;
import entity.InputOrder;
import entity.Material;
import entity.OutputDetails;
import entity.OutputOrder;
import entity.PurchaseDetails;
import entity.PurchaseOrder;
import service.InputService;
import service.OutputService;

@Controller
@ParentPackage("jasperreports-default")
@Namespace("/outputJasper")
public class OutputJasperAction extends ActionSupport {
    private List list;// 循环的参数列表
    private HashMap params;// 非循环的参数map
    private String outputOrderid;

    @Resource
    private OutputService outputService;

    @Action(value = "outputOrder", results = { @Result(name = "outputOrder", type = "jasper", params = {
            "location", "/jasper/OutputReport.jasper", "format", "HTML",
            "dataSource", "list", "reportParameters", "params",
            "imageServletUrl", "/servlets/image?image=", "contentDisposition",
            "inline" }) })
    @SysControllerLogAnnotation(desc = "生成出库单")
    public String inputOrder() {
        OutputOrder ood = outputService.getOrderById(outputOrderid);
        params = new HashMap();
        params.put("id", outputOrderid);
        params.put("outputtime", ood.getOuttime());
        params.put("operator", ood.getCreater().getUsername());
        List<OutputDetails> opdList = outputService
                .getDetailsListById(outputOrderid);
        list = new ArrayList();
        for (OutputDetails outputDetails : opdList) {
            HashMap map = new HashMap();
            Material m = outputDetails.getMaterial();
            map.put("materialid", m.getId());
            map.put("materialname", m.getName());
            map.put("assetno", outputDetails.getAssetno());
            map.put("category", m.getCategory());
            map.put("model", m.getModel());
            map.put("specification", m.getSpecification());
            map.put("unit", m.getUnit());
            map.put("amount", outputDetails.getAmount());
            list.add(map);
        }
        return "outputOrder";
    }

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }

    public HashMap getParams() {
        return params;
    }

    public void setParams(HashMap params) {
        this.params = params;
    }

    public String getOutputOrderid() {
        return outputOrderid;
    }

    public void setOutputOrderid(String outputOrderid) {
        this.outputOrderid = outputOrderid;
    }

}

 

 最后,可以到如下网站学习

http://www.yiibai.com/jasper_reports/

 

 

 

 

 

 

 

  

  

posted @ 2016-10-02 16:51  博学善思。。ljd  阅读(4393)  评论(0编辑  收藏  举报