JasperReport导出excel包含公式

测试所用iReport版本:5.1.0

选中组件,设置“Properties expressions”属性,按照下图设置


 

以下为demo

 

一、准备工作

数据源采用Jason数据源,json串如下

[{"num1":1,"num2":2,"num3":3},{"num1":4,"num2":5,"num3":6}]

将jason串写入文本文件保存,命名为source.txt(文件名可以随意命名)

二、创建json数据源

1、按照如下图中红色标记所示创建数据源

2、创建报表文件

报表demo源码:

<?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/jasperreportshttp://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="formula" language="groovy" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="e34b8fc1-2198-40cb-a035-44cbb6a87f02">
 <property name="ireport.zoom" value="1.0"/>
 <property name="ireport.x" value="0"/>
 <property name="ireport.y" value="0"/>
 <queryString language="JSON">
  <![CDATA[]]>
 </queryString>
 <field name="num1" class="java.math.BigDecimal"/>
 <field name="num2" class="java.math.BigDecimal"/>
 <field name="num3" class="java.math.BigDecimal"/>
 <variable name="sum" class="java.math.BigDecimal">
  <variableExpression><![CDATA[$F{num1}+$F{num2}+$F{num3}]]></variableExpression>
 </variable>
 <variable name="seq" class="java.lang.Integer">
  <variableExpression><![CDATA[$V{seq}+1]]></variableExpression>
  <initialValueExpression><![CDATA[2]]></initialValueExpression>
 </variable>
 <variable name="sum_num1" class="java.math.BigDecimal" calculation="Sum">
  <variableExpression><![CDATA[$F{num1}]]></variableExpression>
 </variable>
 <variable name="sum_num2" class="java.math.BigDecimal" calculation="Sum">
  <variableExpression><![CDATA[$F{num2}]]></variableExpression>
 </variable>
 <variable name="sum_num3" class="java.math.BigDecimal" calculation="Sum">
  <variableExpression><![CDATA[$F{num3}]]></variableExpression>
 </variable>
 <title>
  <band height="79" splitType="Stretch">
   <staticText>
    <reportElement uuid="9b6ef208-aa91-4f93-ac3d-70330daeda64" x="0" y="0" width="555" height="79"/>
    <textElement textAlignment="Center" verticalAlignment="Middle">
     <font size="24" isBold="true"/>
    </textElement>
    <text><![CDATA[Excel导出公式]]></text>
   </staticText>
  </band>
 </title>
 <columnHeader>
  <band height="40">
   <staticText>
    <reportElement uuid="90cbbfe8-5fb4-45f5-8929-17891b238cad" x="0" y="0" width="110" height="40"/>
    <box>
     <pen lineWidth="0.5"/>
     <topPen lineWidth="0.5"/>
     <leftPen lineWidth="0.5"/>
     <bottomPen lineWidth="0.5"/>
     <rightPen lineWidth="0.5"/>
    </box>
    <textElement textAlignment="Center" verticalAlignment="Middle">
     <font size="24" isBold="true"/>
    </textElement>
    <text><![CDATA[列一]]></text>
   </staticText>
   <staticText>
    <reportElement uuid="41f6498a-46fc-48be-b650-3fafc8a9659b" x="110" y="0" width="110" height="40"/>
    <box>
     <pen lineWidth="0.5"/>
     <topPen lineWidth="0.5"/>
     <leftPen lineWidth="0.5"/>
     <bottomPen lineWidth="0.5"/>
     <rightPen lineWidth="0.5"/>
    </box>
    <textElement textAlignment="Center" verticalAlignment="Middle">
     <font size="24" isBold="true"/>
    </textElement>
    <text><![CDATA[列二]]></text>
   </staticText>
   <staticText>
    <reportElement uuid="28884ac5-f994-468a-a26f-c299c712ba3d" x="220" y="0" width="110" height="40"/>
    <box>
     <pen lineWidth="0.5"/>
     <topPen lineWidth="0.5"/>
     <leftPen lineWidth="0.5"/>
     <bottomPen lineWidth="0.5"/>
     <rightPen lineWidth="0.5"/>
    </box>
    <textElement textAlignment="Center" verticalAlignment="Middle">
     <font size="24" isBold="true"/>
    </textElement>
    <text><![CDATA[列三]]></text>
   </staticText>
   <staticText>
    <reportElement uuid="046748b0-c8bd-4092-98dc-8b0b869c34e4" x="330" y="0" width="110" height="40"/>
    <box>
     <pen lineWidth="0.5"/>
     <topPen lineWidth="0.5"/>
     <leftPen lineWidth="0.5"/>
     <bottomPen lineWidth="0.5"/>
     <rightPen lineWidth="0.5"/>
    </box>
    <textElement textAlignment="Center" verticalAlignment="Middle">
     <font size="24" isBold="true"/>
    </textElement>
    <text><![CDATA[总和]]></text>
   </staticText>
  </band>
 </columnHeader>
 <detail>
  <band height="39" splitType="Stretch">
   <textField>
    <reportElement uuid="140b0470-20b3-442e-a8d6-9b668fe86c95" x="0" y="0" width="110" height="39"/>
    <box>
     <pen lineWidth="0.5"/>
     <topPen lineWidth="0.5"/>
     <leftPen lineWidth="0.5"/>
     <bottomPen lineWidth="0.5"/>
     <rightPen lineWidth="0.5"/>
    </box>
    <textElement textAlignment="Center" verticalAlignment="Middle">
     <font size="24"/>
    </textElement>
    <textFieldExpression><![CDATA[$F{num1}]]></textFieldExpression>
   </textField>
   <textField>
    <reportElement uuid="2a951d49-085b-44da-a4ea-32cbe4b35ebb" x="110" y="0" width="110" height="39"/>
    <box>
     <pen lineWidth="0.5"/>
     <topPen lineWidth="0.5"/>
     <leftPen lineWidth="0.5"/>
     <bottomPen lineWidth="0.5"/>
     <rightPen lineWidth="0.5"/>
    </box>
    <textElement textAlignment="Center" verticalAlignment="Middle">
     <font size="24"/>
    </textElement>
    <textFieldExpression><![CDATA[$F{num2}]]></textFieldExpression>
   </textField>
   <textField>
    <reportElement uuid="febc7825-b3b3-4ac5-b4ea-b164cd75c1ef" x="220" y="0" width="110" height="39"/>
    <box>
     <pen lineWidth="0.5"/>
     <topPen lineWidth="0.5"/>
     <leftPen lineWidth="0.5"/>
     <bottomPen lineWidth="0.5"/>
     <rightPen lineWidth="0.5"/>
    </box>
    <textElement textAlignment="Center" verticalAlignment="Middle">
     <font size="24"/>
    </textElement>
    <textFieldExpression><![CDATA[$F{num3}]]></textFieldExpression>
   </textField>
   <textField>
    <reportElement uuid="69f166df-0dfc-40bc-9e0a-763ea54dd326" x="330" y="0" width="110" height="39">
     <propertyExpression name="net.sf.jasperreports.export.xls.formula"><![CDATA["sum(A"+$V{seq}+":C"+$V{seq}+")"]]></propertyExpression>
    </reportElement>
    <box>
     <pen lineWidth="0.5"/>
     <topPen lineWidth="0.5"/>
     <leftPen lineWidth="0.5"/>
     <bottomPen lineWidth="0.5"/>
     <rightPen lineWidth="0.5"/>
    </box>
    <textElement textAlignment="Center" verticalAlignment="Middle">
     <font size="24"/>
    </textElement>
    <textFieldExpression><![CDATA[$V{sum}]]></textFieldExpression>
   </textField>
  </band>
 </detail>
 <summary>
  <band height="37">
   <textField>
    <reportElement uuid="faca8263-bc99-4f67-87dd-0108c1315d6f" x="0" y="0" width="110" height="37">
     <propertyExpression name="net.sf.jasperreports.export.xls.formula"><![CDATA["sum(A3:A"+($V{REPORT_COUNT}+2)+")"]]></propertyExpression>
    </reportElement>
    <box>
     <pen lineWidth="0.5"/>
     <topPen lineWidth="0.5"/>
     <leftPen lineWidth="0.5"/>
     <bottomPen lineWidth="0.5"/>
     <rightPen lineWidth="0.5"/>
    </box>
    <textElement textAlignment="Center" verticalAlignment="Middle">
     <font size="24"/>
    </textElement>
    <textFieldExpression><![CDATA[$V{sum_num1}]]></textFieldExpression>
   </textField>
   <textField>
    <reportElement uuid="069be1f9-251c-448e-9e74-461f8501af5b" x="110" y="0" width="110" height="37">
     <propertyExpression name="net.sf.jasperreports.export.xls.formula"><![CDATA["sum(B3:B"+($V{REPORT_COUNT}+2)+")"]]></propertyExpression>
    </reportElement>
    <box>
     <pen lineWidth="0.5"/>
     <topPen lineWidth="0.5"/>
     <leftPen lineWidth="0.5"/>
     <bottomPen lineWidth="0.5"/>
     <rightPen lineWidth="0.5"/>
    </box>
    <textElement textAlignment="Center" verticalAlignment="Middle">
     <font size="24"/>
    </textElement>
    <textFieldExpression><![CDATA[$V{sum_num2}]]></textFieldExpression>
   </textField>
   <textField>
    <reportElement uuid="8674cfec-6d2f-4940-991b-f13f3096e2f2" x="220" y="0" width="110" height="37">
     <propertyExpression name="net.sf.jasperreports.export.xls.formula"><![CDATA["sum(C3:C"+($V{REPORT_COUNT}+2)+")"]]></propertyExpression>
    </reportElement>
    <box>
     <pen lineWidth="0.5"/>
     <topPen lineWidth="0.5"/>
     <leftPen lineWidth="0.5"/>
     <bottomPen lineWidth="0.5"/>
     <rightPen lineWidth="0.5"/>
    </box>
    <textElement textAlignment="Center" verticalAlignment="Middle">
     <font size="24"/>
    </textElement>
    <textFieldExpression><![CDATA[$V{sum_num3}]]></textFieldExpression>
   </textField>
  </band>
 </summary>
</jasperReport>

将以上源码复制,存入文本文件,将文本文件命名为:formular.jrxml,再用iReport打开此报表文件即可

可以进行预览,选中Preview->XLS Preview,再进行预览,即可预览xls公式效果

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章