首页 > 代码库 > jasperreport使用动态jason数据源

jasperreport使用动态jason数据源

前段时间一直在看DynamicReport,使用xml作为数据源,看了好多资料。然后说是要用jason作为数据源,找了好久DynamicReport没发现支持jason。快速切换到了jasper。这里使用到了.jxml模板和json相结合,给出代码如下:

本文版翻译权归__馋猫和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。

参考链接:http://bbs.csdn.net/topics/390423061?page=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="templatedesign1" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="575" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="e52850f2-70ba-4ac8-b6d8-2cac0212a9b1">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <queryString language="json">
        <![CDATA[sales.item]]>
    </queryString>
    <field name="quotationNo1" class="java.lang.String"/>
    <field name="quotationNo" class="java.lang.String"/>
    <field name="refernceNo" class="java.lang.String"/>
    <field name="quotationDate" class="java.lang.String"/>
    <field name="pic" class="java.lang.String"/>
    <field name="email" class="java.lang.String"/>
    <field name="tel" class="java.lang.String"/>
    <field name="mobile" class="java.lang.String"/>
    <field name="to" class="java.lang.String"/>
    <field name="attn" class="java.lang.String"/>
    <field name="mv" class="java.lang.String"/>
    <field name="imo" class="java.lang.String"/>
    <field name="equipment" class="java.lang.String"/>
    <field name="project" class="java.lang.String"/>
    <field name="count" class="java.lang.Integer"/>
    <field name="price" class="java.lang.Integer"/>
    <field name="total" class="java.lang.Integer"/>
    <field name="ps" class="java.lang.String"/>
    <field name="serviceCharge" class="java.lang.String"/>
    <field name="materialCharge" class="java.lang.String"/>
    <field name="allCharge" class="java.lang.String"/>
    <group name="groupName"/>
    <title>
        <band height="240">
            <textField isStretchWithOverflow="true">
                <reportElement x="365" y="106" width="200" height="20" uuid="eb0cfa20-647b-455c-a812-fffa3399c071"/>
                <textElement textAlignment="Left" verticalAlignment="Middle">
                    <font fontName="微软雅黑"/>
                </textElement>
                <textFieldExpression><![CDATA["Quotation No:  "+$F{quotationNo}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="19" y="106" width="200" height="20" uuid="48bb6b51-6811-40a3-bdc6-79b314716885"/>
                <textElement textAlignment="Left" verticalAlignment="Middle">
                    <font fontName="微软雅黑"/>
                </textElement>
                <textFieldExpression><![CDATA["Your Refernce No.:  "+$F{refernceNo}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="365" y="127" width="200" height="20" uuid="efbeda2f-afbc-4585-aacb-b9c7ccc10468"/>
                <textElement textAlignment="Left" verticalAlignment="Middle">
                    <font fontName="微软雅黑"/>
                </textElement>
                <textFieldExpression><![CDATA["Quotation Date:  "+$F{quotationDate}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="365" y="146" width="200" height="20" uuid="a6ba9479-b1a2-4fe3-afbf-85ccc34b06bd"/>
                <textElement textAlignment="Left" verticalAlignment="Middle">
                    <font fontName="微软雅黑"/>
                </textElement>
                <textFieldExpression><![CDATA["PIC:  "+$F{pic}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="365" y="166" width="200" height="20" uuid="ade90e4f-db8d-4951-9020-b9ee5aa3bf95"/>
                <textElement textAlignment="Left" verticalAlignment="Middle">
                    <font fontName="微软雅黑"/>
                </textElement>
                <textFieldExpression><![CDATA["Email:  "+$F{email}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="365" y="187" width="200" height="20" uuid="8a7c9e25-e680-4239-a971-b8ef009d0e8c"/>
                <textElement textAlignment="Left" verticalAlignment="Middle">
                    <font fontName="微软雅黑"/>
                </textElement>
                <textFieldExpression><![CDATA["Tel:  "+$F{tel}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="365" y="208" width="200" height="20" uuid="1bc1ce84-ab53-49b7-8a9a-e3025d5fe03a"/>
                <textElement textAlignment="Left" verticalAlignment="Middle">
                    <font fontName="微软雅黑"/>
                </textElement>
                <textFieldExpression><![CDATA["Mobile:  "+$F{mobile}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="19" y="146" width="200" height="20" uuid="dbe5f9f1-0a41-40b8-b4a3-fc35f1bf8c09"/>
                <textElement textAlignment="Left" verticalAlignment="Middle">
                    <font fontName="微软雅黑"/>
                </textElement>
                <textFieldExpression><![CDATA["To:  "+$F{to}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="19" y="166" width="200" height="20" uuid="0b1f9b9b-0f49-44f5-bb4b-45e885347d72"/>
                <textElement textAlignment="Left" verticalAlignment="Middle">
                    <font fontName="微软雅黑"/>
                </textElement>
                <textFieldExpression><![CDATA["Attn:  "+$F{attn}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="19" y="187" width="200" height="20" uuid="429c115d-f7da-423d-99af-dcc610928e53"/>
                <textElement textAlignment="Left" verticalAlignment="Middle">
                    <font fontName="微软雅黑"/>
                </textElement>
                <textFieldExpression><![CDATA["M.V.:  "+$F{mv}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="19" y="208" width="200" height="20" uuid="6acc98a1-e76a-44f3-a3f2-93f2aae88bfa"/>
                <textElement textAlignment="Left" verticalAlignment="Middle">
                    <font fontName="微软雅黑"/>
                </textElement>
                <textFieldExpression><![CDATA["IMO:  "+$F{imo}]]></textFieldExpression>
            </textField>
            <line>
                <reportElement x="19" y="85" width="542" height="1" uuid="71e5f04d-9440-4e60-aca5-5c5d1bec0774"/>
            </line>
            <line>
                <reportElement x="19" y="105" width="542" height="1" uuid="e809947c-54f3-4a41-a659-83c1e7f54004"/>
            </line>
            <line>
                <reportElement x="19" y="231" width="542" height="1" uuid="3165ccd4-6bd8-4437-b0e3-7134d8e4b02a"/>
            </line>
        </band>
    </title>
    <columnHeader>
        <band height="20">
            <rectangle>
                <reportElement x="19" y="0" width="56" height="20" uuid="90f1f241-3958-415b-a38a-13bca569dda8"/>
            </rectangle>
            <rectangle>
                <reportElement x="75" y="0" width="84" height="20" uuid="440c1ab9-5646-4d13-bfa3-19fe29f7c820"/>
            </rectangle>
            <rectangle>
                <reportElement x="158" y="0" width="200" height="20" uuid="df915095-5af0-485f-8ac8-a6b423948bbf"/>
            </rectangle>
            <rectangle>
                <reportElement x="358" y="0" width="56" height="20" uuid="53ea7c38-ba4c-4192-8511-1efaf71f7cfb"/>
            </rectangle>
            <rectangle>
                <reportElement x="413" y="0" width="84" height="20" uuid="1f0566e6-27a1-4f6f-b2cb-bb340da62642"/>
            </rectangle>
            <rectangle>
                <reportElement x="495" y="0" width="70" height="20" uuid="ba52d696-893a-4290-a72b-ba30aa6dcc1a"/>
            </rectangle>
            <staticText>
                <reportElement x="19" y="0" width="56" height="20" uuid="9bcf5615-aa7e-4517-9180-7c3c38eebb5e"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="微软雅黑"/>
                </textElement>
                <text><![CDATA[序号]]></text>
            </staticText>
            <staticText>
                <reportElement x="75" y="0" width="84" height="20" uuid="0a2e3a69-31e3-4000-b371-a3133d2991dc"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="微软雅黑"/>
                </textElement>
                <text><![CDATA[设备]]></text>
            </staticText>
            <staticText>
                <reportElement x="158" y="0" width="200" height="20" uuid="5df68d5c-9473-4dc0-a151-ee5f8cfc684a"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="微软雅黑"/>
                </textElement>
                <text><![CDATA[项目]]></text>
            </staticText>
            <staticText>
                <reportElement x="358" y="0" width="56" height="20" uuid="59b80257-4212-4b10-8c2f-3084e51743c2"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="微软雅黑"/>
                </textElement>
                <text><![CDATA[数量]]></text>
            </staticText>
            <staticText>
                <reportElement x="413" y="0" width="84" height="20" uuid="a9c16c7d-bdfe-489a-9b3b-4bb5b3656a8c"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="微软雅黑"/>
                </textElement>
                <text><![CDATA[单价(RMB)]]></text>
            </staticText>
            <staticText>
                <reportElement x="495" y="0" width="70" height="20" uuid="e6a2a4ba-4948-4c6a-b4e5-f88ab24f954e"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="微软雅黑"/>
                </textElement>
                <text><![CDATA[合计(RMB)]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="20">
            <rectangle>
                <reportElement x="19" y="0" width="56" height="20" uuid="d949ecef-26b7-4a99-8608-c4d07aa4d6d0"/>
            </rectangle>
            <rectangle>
                <reportElement x="75" y="0" width="84" height="20" uuid="e344b5f1-6b43-465e-a2c5-b2d81b9a788f"/>
            </rectangle>
            <rectangle>
                <reportElement x="158" y="0" width="200" height="20" uuid="1ce0bea6-78d5-4f0a-99cd-7a6e774a83ed"/>
            </rectangle>
            <rectangle>
                <reportElement x="358" y="0" width="56" height="20" uuid="768f5e11-0cae-4d71-8b86-47a6c5254505"/>
            </rectangle>
            <rectangle>
                <reportElement x="413" y="0" width="84" height="20" uuid="51d69656-af3f-41a2-a897-799a024456cc"/>
            </rectangle>
            <rectangle>
                <reportElement x="495" y="0" width="70" height="20" uuid="729a90ba-1ded-4c87-9ef5-f91afa11d6b9"/>
            </rectangle>
            <textField>
                <reportElement x="19" y="0" width="56" height="20" uuid="c00767a0-a5f9-41c5-951a-2190fdb076d6"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="微软雅黑" size="8"/>
                </textElement>
                <textFieldExpression><![CDATA[""+$V{REPORT_COUNT}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="75" y="0" width="84" height="20" uuid="de4daa80-8455-4786-abc4-629beae2f0eb"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="微软雅黑" size="8"/>
                </textElement>
                <textFieldExpression><![CDATA[""+$F{equipment}]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true" isBlankWhenNull="false">
                <reportElement x="158" y="0" width="200" height="20" uuid="14f3f1d0-a7b8-48c8-a220-a75db323fd52"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="微软雅黑" size="8"/>
                </textElement>
                <textFieldExpression><![CDATA[" "]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="358" y="0" width="56" height="20" uuid="3057876d-467f-4543-b31e-e2ef1b72bb88"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="微软雅黑" size="8"/>
                </textElement>
                <textFieldExpression><![CDATA[""+$F{count}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="413" y="0" width="84" height="20" uuid="9f609834-c546-4b49-a704-37d3ac94e57a"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="微软雅黑" size="8"/>
                </textElement>
                <textFieldExpression><![CDATA[""+$F{price}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="495" y="0" width="70" height="20" uuid="2dfae995-90a8-4fb7-909a-bbf8765ae820"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font fontName="微软雅黑" size="8"/>
                </textElement>
                <textFieldExpression><![CDATA[""+$F{total}]]></textFieldExpression>
            </textField>
            <rectangle>
                <reportElement x="565" y="0" width="17" height="20" uuid="ddb23140-6fbf-4f1b-abff-7de2fb4f606d"/>
            </rectangle>
        </band>
    </detail>
    <summary>
        <band height="160">
            <staticText>
                <reportElement x="19" y="0" width="200" height="20" uuid="e7c5057f-9ecf-4dee-bd7b-f134739d4e01"/>
                <textElement textAlignment="Left" verticalAlignment="Middle">
                    <font fontName="微软雅黑" isBold="true"/>
                </textElement>
                <text><![CDATA[注:]]></text>
            </staticText>
            <staticText>
                <reportElement x="19" y="51" width="543" height="20" uuid="5c083135-ba8d-49cf-b5b7-d02c0ef0ec5a"/>
                <textElement textAlignment="Left" verticalAlignment="Middle">
                    <font fontName="微软雅黑" size="10"/>
                </textElement>
                <text><![CDATA[Please mention our quotzation no. in you purchasing order]]></text>
            </staticText>
            <staticText>
                <reportElement x="19" y="72" width="543" height="20" uuid="86e69270-73f5-4fe9-9c0d-0ec9c56e250d"/>
                <textElement textAlignment="Left" verticalAlignment="Top">
                    <font fontName="微软雅黑" size="10"/>
                </textElement>
                <text><![CDATA[谢谢询价!确认时请使用报价号,谢谢]]></text>
            </staticText>
            <textField isStretchWithOverflow="true">
                <reportElement x="19" y="21" width="543" height="29" uuid="11b0df67-f879-4bcb-9b47-1077cdef4d9b"/>
                <textElement textAlignment="Left" verticalAlignment="Top">
                    <font fontName="微软雅黑" size="8"/>
                </textElement>
                <textFieldExpression><![CDATA[""+$F{ps}]]></textFieldExpression>
            </textField>
            <textField isStretchWithOverflow="true">
                <reportElement x="362" y="93" width="200" height="20" uuid="1569be55-9fbe-469d-ba7d-ad605559f56d"/>
                <textElement textAlignment="Left" verticalAlignment="Top">
                    <font fontName="微软雅黑" size="10"/>
                </textElement>
                <textFieldExpression><![CDATA["服务费小计:"+$F{serviceCharge}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="362" y="114" width="200" height="20" uuid="4e3b94da-6a9c-49e5-98c9-71d3f942d88c"/>
                <textElement textAlignment="Left" verticalAlignment="Top">
                    <font fontName="微软雅黑" size="10"/>
                </textElement>
                <textFieldExpression><![CDATA["材料费小计: "+$F{materialCharge}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="362" y="134" width="200" height="20" uuid="1f6d2430-5c08-4a91-8865-2a81489a358e"/>
                <textElement textAlignment="Left" verticalAlignment="Top">
                    <font fontName="微软雅黑" size="10"/>
                </textElement>
                <textFieldExpression><![CDATA["总计: "+$F{allCharge}]]></textFieldExpression>
            </textField>
        </band>
    </summary>
</jasperReport>
NewPaneTest.jrxml

json数据源:

技术分享
public class JSONSource {
    public final static String json = "{\"sales\": {\"item\": ["
            + "{\"-id\": \"1\",\"equipment\": \"测探仪\",\"project\": \"维修\",\"count\": \"1\",\"price\": \"2500\",\"total\": \"2500\",\"ps\": \"1)贵司雷达申请单是两台S波段雷达,应该是不准确的,因此我们报价一个X波段磁控管,一个S波段磁控管;2)罗经球寿命是3年,其中有一台罗经球使用2年半,建议更换该罗经球,从而两台罗经球使用时间不同,确保两台罗经球不至于由于使用时间临近而同时出现故障;3)以上工程,正常我们会在3天内完成,如果延长时间,我们会适当增加一些交通费\", \"refernceNo\": \"1000254852\", \"quotationNo\": \"15TMKQS090701\",\"quotationDate\": \"2016-11-21 13:48\",\"to\": \"天津中散船舶管理有限公司\",\"pic\": \"Kang Baoyi(康宝义)\",\"attn\": \"马洪元主管\",\"email\": \"kangbaoyi@cosbulk.com\",\"mv\": \"China Energy\",\"imo\": \"9495064\",\"tel\": \"(86)22 65705801\",\"mobile\": \"(86) 13602170331\",\"serviceCharge\": \"25600\",\"materialCharge\": \"91590\",\"allCharge\": \"117190\"},"
            + "{ \"-id\": \"1\", \"equipment\": \"测探仪\",\"project\": \"探头\",\"count\": \"1\",\"price\": \"12500\",\"total\": \"12500\"},"
            + " {\"-id\": \"1\",\"equipment\": \"电罗经\",\"project\": \"保养\",\"count\": \"2\",\"price\": \"2200\",\"total\": \"4400\"},"
            + "{\"-id\": \"1\",\"equipment\": \"交通费\",\"project\": \" \",\"count\": \"1\",\"price\": \"1500\",\"total\": \"1500\",\"ps\": \"1)贵司雷达申请单是两台S波段雷达,应该是不准确的,因此我们报价一个X波段磁控管,一个S波段磁控管;2)罗经球寿命是3年,其中有一台罗经球使用2年半,建议更换该罗经球,从而两台罗经球使用时间不同,确保两台罗经球不至于由于使用时间临近而同时出现故障;3)以上工程,正常我们会在3天内完成,如果延长时间,我们会适当增加一些交通费\",\"refernceNo\": \"1000254852\",\"quotationNo\": \"15TMKQS090701\",\"quotationDate\": \"2016-11-21 13:48\",\"to\": \"天津中散船舶管理有限公司\",\"pic\": \"Kang Baoyi(康宝义)\",\"attn\": \"马洪元主管\",\"email\": \"kangbaoyi@cosbulk.com\",\"mv\": \"China Energy\",\"imo\": \"9495064\",\"tel\": \"(86)22 65705801\",\"mobile\": \"(86) 13602170331\",\"serviceCharge\": \"25600\",\"materialCharge\": \"91590\",\"allCharge\": \"117190\"}"
            + "]}}";}
JSONSource

主代码:

技术分享
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Locale;

import net.sf.dynamicreports.jasper.base.export.JasperPdfExporter;
import net.sf.jasperreports.engine.JRParameter;
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.JasperReport;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter;
import net.sf.jasperreports.engine.query.JsonQueryExecuterFactory;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.export.ExporterInput;
import net.sf.jasperreports.export.OutputStreamExporterOutput;
import net.sf.jasperreports.export.SimpleExporterInput;
import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput;
import net.sf.jasperreports.export.SimpleXlsxReportConfiguration;
import net.sf.jasperreports.view.JasperViewer;

public class testJsonBuffer {

    public static void main(String[] args) {
        JasperDesign design;
        try {
            design = JRXmlLoader.load("F:\\NewPaneTest.jrxml");
            HashMap<String, Object> paramsMap = new HashMap<String, Object>();

            // 第二种配置数据源的方式 InputStream
            InputStream is = new ByteArrayInputStream(JSONSource.json1.getBytes("UTF-8"));
            paramsMap.put("JSON_INPUT_STREAM", is);
            // 配置其他参数
            paramsMap.put(JsonQueryExecuterFactory.JSON_LOCALE, Locale.ENGLISH);
            paramsMap.put(JRParameter.REPORT_LOCALE, Locale.US);
            // 加载模板
            JasperReport report = JasperCompileManager.compileReport(design);
            // 填充数据
            JasperPrint print = JasperFillManager.fillReport(report, paramsMap);
            // 预览显示
            JasperViewer.viewReport(print);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}
testJsonBuffer

 

注意:

模板里面的查询语句要设为json,字段要对应上

 本文版翻译权归__馋猫和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。

jasperreport使用动态jason数据源