本文主要是介绍Jasperreports+jaspersoft studio学习教程(八)- 报表分页和大量数据内存处理,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
9.1 设计报表模板
9.1.1 使用Table组件新建模板(步骤参考教程七)如下:
9.1.2 模板自带变量
$V{PAGE_NUMBER} :代表当前页数(可以是页码也可以是页数,通过TextField的计算时间的不同值来设置)
$V{PAGE_COUNT} :当前页面中记录的数目
$V{groupname_COUNT} : 代表当前组的记录数
$V{COLUMN_NUMBER} :列号码
$V{COLUMN_COUNT} :当前列中记录的数目(是当前页里行的序号,在新页上会重置为1)
$V{REPORT_COUNT} :当前文档中数据源记录数目。
这里主要使用 $V{PAGE_NUMBER} 来做报表分页
对于$V{PAGE_NUMBER}:当evaluationTime="Now"时,即是计算当页所在哪一页。 (now:即时执行此表达式)
当evaluationTime="Report"时,计算最后一页,即总页数。(Report:整个报表执行完后才执行此表达式)
9.1.3 拖动6个Text Field组件到Page Footer Band中
设置 第几页 ,注意 其 Evaluation Time 选中 Now。
设置 共几页 ,注意 其 Evaluation Time 选中 Report。
9.1.4 设置 首页、上一页、下一页、末页 需要使用超链接
新建一个模板,把 首页 拷贝过去(这里使用DemoReport1.jrxml)
点击首页显示Properties ,搜索HyperLink PageException
设置 链接 为 “https://www.baidu.com/”
保存后,以 HTML 方式 Preview 后,点击首页跳转到百度。
9.1.5 设置 首页、上一页、下一页、末页
在 Hyperlink PageExpression 框中输入时需要注意,我们需要设定变量才能正确的导向是哪一页
以下是各选择在HyperlinkPage Expression框中输入的信息
首页 : "/JasperWeb/JasperServlet9?page=0"
上一页 : "/JasperWeb/JasperServlet9?page="+($V{PAGE_NUMBER}-Long.valueOf("1"))
下一页 : "/JasperWeb/JasperServlet9?page="+($V{PAGE_NUMBER}+Long.valueOf("1"))
末页 : "/JasperWeb/JasperServlet9?page=lastPage"
保存模板文件后拷贝到JasperWeb项目中
9.2 编写java代码,实现分页逻辑及大量数据内存处理
访问servlet,如下:
将第三页存储在磁盘中。
DemoReport9.jrxml
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.4.0.final using JasperReports Library version 6.4.1 -->
<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="DemoReport9" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="802" leftMargin="20" rightMargin="20" topMargin="0" bottomMargin="0" uuid="d7c90b7d-72ba-419f-94bd-d1d1ebac1597"><property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/><property name="com.jaspersoft.studio.unit." value="pixel"/><property name="com.jaspersoft.studio.unit.pageHeight" value="pixel"/><property name="com.jaspersoft.studio.unit.pageWidth" value="pixel"/><property name="com.jaspersoft.studio.unit.topMargin" value="pixel"/><property name="com.jaspersoft.studio.unit.bottomMargin" value="pixel"/><property name="com.jaspersoft.studio.unit.leftMargin" value="pixel"/><property name="com.jaspersoft.studio.unit.rightMargin" value="pixel"/><property name="com.jaspersoft.studio.unit.columnWidth" value="pixel"/><property name="com.jaspersoft.studio.unit.columnSpacing" value="pixel"/><style name="Table_TH" mode="Opaque" backcolor="#F0F8FF"><box><pen lineWidth="0.5" lineColor="#000000"/><topPen lineWidth="0.5" lineColor="#000000"/><leftPen lineWidth="0.5" lineColor="#000000"/><bottomPen lineWidth="0.5" lineColor="#000000"/><rightPen lineWidth="0.5" lineColor="#000000"/></box></style><style name="Table_CH" mode="Opaque" backcolor="#BFE1FF"><box><pen lineWidth="0.5" lineColor="#000000"/><topPen lineWidth="0.5" lineColor="#000000"/><leftPen lineWidth="0.5" lineColor="#000000"/><bottomPen lineWidth="0.5" lineColor="#000000"/><rightPen lineWidth="0.5" lineColor="#000000"/></box></style><style name="Table_TD" mode="Opaque" backcolor="#FFFFFF"><box><pen lineWidth="0.5" lineColor="#000000"/><topPen lineWidth="0.5" lineColor="#000000"/><leftPen lineWidth="0.5" lineColor="#000000"/><bottomPen lineWidth="0.5" lineColor="#000000"/><rightPen lineWidth="0.5" lineColor="#000000"/></box></style><subDataset name="Dataset1" uuid="38de2d28-85cc-4464-90e5-b3a328724707"><property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/><queryString><![CDATA[]]></queryString><field name="userId" class="java.lang.Integer"/><field name="userName" class="java.lang.String"/><field name="grade" class="java.lang.String"/><field name="college" class="java.lang.String"/><field name="mobile" class="java.lang.String"/></subDataset><queryString><![CDATA[]]></queryString><field name="tableData" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/><field name="date" class="java.lang.String"/><title><band height="60" splitType="Stretch"><property name="com.jaspersoft.studio.unit.height" value="pixel"/><textField><reportElement x="0" y="10" width="800" height="40" uuid="b96cd39e-8959-469c-bdb2-747d5750e575"><property name="com.jaspersoft.studio.unit.y" value="pixel"/><property name="com.jaspersoft.studio.unit.width" value="pixel"/><property name="com.jaspersoft.studio.unit.height" value="pixel"/></reportElement><textElement textAlignment="Center" verticalAlignment="Middle"><font fontName="微软雅黑" size="24"/></textElement><textFieldExpression><![CDATA["用户信息表"]]></textFieldExpression></textField></band></title><pageHeader><band height="30" splitType="Stretch"><property name="com.jaspersoft.studio.unit.height" value="pixel"/><textField><reportElement x="640" y="0" width="160" height="30" uuid="aa748f9a-c2ff-4b50-bf34-b586e1e221cd"/><textElement textAlignment="Right" verticalAlignment="Middle"><font fontName="微软雅黑"/></textElement><textFieldExpression><![CDATA["日期:"+$F{date}]]></textFieldExpression></textField></band></pageHeader><detail><band height="50" splitType="Stretch"><property name="com.jaspersoft.studio.unit.height" value="pixel"/><componentElement><reportElement x="0" y="0" width="800" height="50" uuid="1adbebf1-674c-4aac-9b79-2afa0b77f6e6"><property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/><property name="com.jaspersoft.studio.table.style.table_header" value="Table_TH"/><property name="com.jaspersoft.studio.table.style.column_header" value="Table_CH"/><property name="com.jaspersoft.studio.table.style.detail" value="Table_TD"/><property name="com.jaspersoft.studio.unit.x" value="pixel"/><property name="com.jaspersoft.studio.unit.y" value="pixel"/><property name="com.jaspersoft.studio.unit.width" value="pixel"/><property name="com.jaspersoft.studio.unit.height" value="pixel"/></reportElement><jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd"><datasetRun subDataset="Dataset1" uuid="65713b63-7b23-4cef-9297-cd0b0a50742d"><dataSourceExpression><![CDATA[$F{tableData}]]></dataSourceExpression></datasetRun><jr:column width="160" uuid="adf9bff4-2683-400d-8c4d-0c0984e93f0d"><property name="com.jaspersoft.studio.unit.height" value="pixel"/><property name="com.jaspersoft.studio.components.table.model.column.name" value="Column1"/><property name="com.jaspersoft.studio.unit.width" value="pixel"/><jr:columnHeader style="Table_CH" height="30" rowSpan="1"><property name="com.jaspersoft.studio.unit.width" value="px"/><textField><reportElement x="0" y="0" width="160" height="30" uuid="0b36445f-cb93-4cc7-8467-1001e0bc9138"/><textElement textAlignment="Center" verticalAlignment="Middle"><font fontName="微软雅黑" size="14"/></textElement><textFieldExpression><![CDATA["学号"]]></textFieldExpression></textField></jr:columnHeader><jr:detailCell style="Table_TD" height="20"><property name="com.jaspersoft.studio.unit.width" value="px"/><property name="com.jaspersoft.studio.unit.height" value="px"/><textField><reportElement x="0" y="0" width="160" height="20" uuid="06cfd83a-dde4-44d9-bc68-52fe7ae14a8e"/><textElement textAlignment="Center" verticalAlignment="Middle"><font fontName="微软雅黑"/></textElement><textFieldExpression><![CDATA[$F{userId}]]></textFieldExpression></textField></jr:detailCell></jr:column><jr:column width="160" uuid="10201ee5-5600-4d8d-966d-e8d3f6976801"><property name="com.jaspersoft.studio.components.table.model.column.name" value="Column2"/><property name="com.jaspersoft.studio.unit.width" value="pixel"/><jr:columnHeader style="Table_CH" height="30" rowSpan="1"><property name="com.jaspersoft.studio.unit.width" value="px"/><textField><reportElement x="0" y="0" width="160" height="30" uuid="14d03642-b623-4b5b-b719-2fe8bbaa0a4e"/><textElement textAlignment="Center" verticalAlignment="Middle"><font fontName="微软雅黑" size="14"/></textElement><textFieldExpression><![CDATA["姓名"]]></textFieldExpression></textField></jr:columnHeader><jr:detailCell style="Table_TD" height="20"><textField><reportElement x="0" y="0" width="160" height="20" uuid="06001ccc-e18d-4f17-b579-151a8a0d1d57"/><textElement textAlignment="Center" verticalAlignment="Middle"><font fontName="微软雅黑"/></textElement><textFieldExpression><![CDATA[$F{userName}]]></textFieldExpression></textField></jr:detailCell></jr:column><jr:column width="160" uuid="3a5875e8-f8d4-4289-a2c0-339f203a0a9c"><property name="com.jaspersoft.studio.components.table.model.column.name" value="Column3"/><property name="com.jaspersoft.studio.unit.width" value="pixel"/><jr:columnHeader style="Table_CH" height="30" rowSpan="1"><property name="com.jaspersoft.studio.unit.width" value="px"/><textField><reportElement x="0" y="0" width="160" height="30" uuid="fecac7e3-9c5f-4ae5-b9bf-e740cb1acaa9"/><textElement textAlignment="Center" verticalAlignment="Middle"><font fontName="微软雅黑" size="14"/></textElement><textFieldExpression><![CDATA["年级"]]></textFieldExpression></textField></jr:columnHeader><jr:detailCell style="Table_TD" height="20"><textField><reportElement x="0" y="0" width="160" height="20" uuid="449cd8b8-e07e-44d6-a654-9382041e19b6"/><textElement textAlignment="Center" verticalAlignment="Middle"><font fontName="微软雅黑"/></textElement><textFieldExpression><![CDATA[$F{grade}]]></textFieldExpression></textField></jr:detailCell></jr:column><jr:column width="160" uuid="8d0acd4a-e38f-452e-8f83-b9f471cc6e6c"><property name="com.jaspersoft.studio.components.table.model.column.name" value="Column4"/><property name="com.jaspersoft.studio.unit.width" value="pixel"/><jr:columnHeader style="Table_CH" height="30" rowSpan="1"><property name="com.jaspersoft.studio.unit.width" value="px"/><textField><reportElement x="0" y="0" width="160" height="30" uuid="71f6ec6b-9fff-4033-a6b3-4859cef156b9"/><textElement textAlignment="Center" verticalAlignment="Middle"><font fontName="微软雅黑" size="14"/></textElement><textFieldExpression><![CDATA["学院"]]></textFieldExpression></textField></jr:columnHeader><jr:detailCell style="Table_TD" height="20"><textField><reportElement x="0" y="0" width="160" height="20" uuid="c4d5d4f7-e9fe-4a19-b8d6-09e8043ea51a"/><textElement textAlignment="Center" verticalAlignment="Middle"><font fontName="微软雅黑"/></textElement><textFieldExpression><![CDATA[$F{college}]]></textFieldExpression></textField></jr:detailCell></jr:column><jr:column width="160" uuid="f05e42f0-7427-4e35-a528-fed13c202a0a"><property name="com.jaspersoft.studio.components.table.model.column.name" value="Column5"/><property name="com.jaspersoft.studio.unit.width" value="pixel"/><jr:columnHeader style="Table_CH" height="30" rowSpan="1"><property name="com.jaspersoft.studio.unit.width" value="px"/><textField><reportElement x="0" y="0" width="160" height="30" uuid="ab76786f-1468-43cb-965e-6469f9f21363"/><textElement textAlignment="Center" verticalAlignment="Middle"><font fontName="微软雅黑" size="14"/></textElement><textFieldExpression><![CDATA["手机"]]></textFieldExpression></textField></jr:columnHeader><jr:detailCell style="Table_TD" height="20"><textField><reportElement x="0" y="0" width="160" height="20" uuid="5db1623e-b62b-449c-82ed-e796243710e7"/><textElement textAlignment="Center" verticalAlignment="Middle"><font fontName="微软雅黑"/></textElement><textFieldExpression><![CDATA[$F{mobile}]]></textFieldExpression></textField></jr:detailCell></jr:column></jr:table></componentElement></band></detail><pageFooter><band height="54" splitType="Stretch"><textField hyperlinkType="Reference"><reportElement x="620" y="5" width="30" height="20" uuid="e77c471c-6f7a-4611-aa73-fb7e3a88c93d"><property name="com.jaspersoft.studio.unit.y" value="pixel"/><property name="com.jaspersoft.studio.unit.height" value="pixel"/><property name="com.jaspersoft.studio.unit.x" value="pixel"/><property name="com.jaspersoft.studio.unit.width" value="pixel"/></reportElement><textElement textAlignment="Center" verticalAlignment="Middle"><font fontName="微软雅黑"/></textElement><textFieldExpression><![CDATA["首页"]]></textFieldExpression><hyperlinkReferenceExpression><![CDATA["/JasperWeb/JasperServlet9?page=0"]]></hyperlinkReferenceExpression></textField><textField hyperlinkType="Reference"><reportElement x="650" y="5" width="50" height="20" uuid="6709103c-8b77-4690-b2e4-468bd8e5544e"><property name="com.jaspersoft.studio.unit.y" value="pixel"/><property name="com.jaspersoft.studio.unit.height" value="pixel"/><property name="com.jaspersoft.studio.unit.x" value="pixel"/><property name="com.jaspersoft.studio.unit.width" value="pixel"/></reportElement><textElement textAlignment="Center" verticalAlignment="Middle"><font fontName="微软雅黑" size="10"/></textElement><textFieldExpression><![CDATA["上一页"]]></textFieldExpression><hyperlinkReferenceExpression><![CDATA["/JasperWeb/JasperServlet9?page="+($V{PAGE_NUMBER}-Long.valueOf("1"))]]></hyperlinkReferenceExpression></textField><textField hyperlinkType="Reference"><reportElement x="700" y="5" width="50" height="20" uuid="4263bf73-f221-41b4-9df2-4eaeea435a9f"><property name="com.jaspersoft.studio.unit.y" value="pixel"/><property name="com.jaspersoft.studio.unit.height" value="pixel"/><property name="com.jaspersoft.studio.unit.x" value="pixel"/><property name="com.jaspersoft.studio.unit.width" value="pixel"/></reportElement><textElement textAlignment="Center" verticalAlignment="Middle"><font fontName="微软雅黑"/></textElement><textFieldExpression><![CDATA["下一页"]]></textFieldExpression><hyperlinkReferenceExpression><![CDATA["/JasperWeb/JasperServlet9?page="+($V{PAGE_NUMBER}+Long.valueOf("1"))]]></hyperlinkReferenceExpression></textField><textField hyperlinkType="Reference"><reportElement x="750" y="5" width="30" height="20" uuid="704e48cb-9208-4703-b260-05662ea65334"><property name="com.jaspersoft.studio.unit.height" value="pixel"/><property name="com.jaspersoft.studio.unit.x" value="pixel"/></reportElement><textElement textAlignment="Center" verticalAlignment="Middle"><font fontName="微软雅黑"/></textElement><textFieldExpression><![CDATA["末页"]]></textFieldExpression><hyperlinkReferenceExpression><![CDATA["/JasperWeb/JasperServlet9?page=lastPage"]]></hyperlinkReferenceExpression></textField><textField><reportElement x="510" y="5" width="50" height="20" uuid="b1b31a99-915b-479b-8eb2-e7496ec9021f"><property name="com.jaspersoft.studio.unit.x" value="pixel"/><property name="com.jaspersoft.studio.unit.y" value="pixel"/><property name="com.jaspersoft.studio.unit.width" value="pixel"/><property name="com.jaspersoft.studio.unit.height" value="pixel"/></reportElement><textElement textAlignment="Center" verticalAlignment="Middle"><font fontName="微软雅黑"/></textElement><textFieldExpression><![CDATA["第"+$V{PAGE_NUMBER}+"页"]]></textFieldExpression></textField><textField evaluationTime="Report"><reportElement x="560" y="5" width="60" height="20" uuid="6f34ea83-3dde-4765-b882-82997673eeaf"><property name="com.jaspersoft.studio.unit.y" value="pixel"/><property name="com.jaspersoft.studio.unit.height" value="pixel"/></reportElement><textElement textAlignment="Center" verticalAlignment="Middle"><font fontName="微软雅黑" size="10"/></textElement><textFieldExpression><![CDATA["共"+$V{PAGE_NUMBER}+"页"]]></textFieldExpression></textField></band></pageFooter>
</jasperReport>
JasperServlet9.java
package com.jasper.web;import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JRParameter;
import net.sf.jasperreports.engine.JasperCompileManager;
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.JRHtmlExporter;
import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
import net.sf.jasperreports.engine.fill.JRFileVirtualizer;import com.jasper.bean.ModelTableSource;
import com.jasper.bean.Users;@WebServlet("/JasperServlet9")
public class JasperServlet9 extends HttpServlet {private static final long serialVersionUID = 1L;public JasperServlet9() {super();}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {Map<String,Object> parameters = new HashMap<String,Object>(16);String pageStr = request.getParameter("page");HttpSession session = request.getSession();int pageIndex = 0;int lastPageIndex = 0;//list数据源List<Users> list = new ArrayList<Users>();for(int i = 1 ; i <= 50 ; i++) {Users users = new Users(i,"name"+i,"grade"+i,"college"+i,"mobile"+i);list.add(users);}ModelTableSource mts = new ModelTableSource();mts.setDate(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));mts.setTableData(new JRBeanCollectionDataSource(list));List<ModelTableSource> mlist = new ArrayList<ModelTableSource>();mlist.add(mts);String jrxmlPath = request.getServletContext().getRealPath("/")+"/jrxml/DemoReport9.jrxml";//由jrxml文件编译后生产jasper文件的路径String jasperPath = request.getServletContext().getRealPath("/")+"/jasper/DemoReport9.jasper";PrintWriter out = null;try {//编译jrxml生产jasper文件JasperCompileManager.compileReportToFile(jrxmlPath, jasperPath);//JasperPrint将数据全部取出,放到session中JasperPrint jasperPrint = (JasperPrint) session.getAttribute("jasperPrint");if(null == jasperPrint) {jasperPrint = this.getJasperPrint(jasperPath, parameters, mlist);session.setAttribute("jasperPrint", jasperPrint);}if(null != jasperPrint.getPages()) {lastPageIndex = jasperPrint.getPages().size() - 1;}if(null == pageStr) {pageStr = "0";}try {pageIndex = Integer.valueOf(pageStr);if(pageIndex > 0) {pageIndex = pageIndex -1 ;}} catch (Exception e) {// 如果得到的非数字字符串if("lastPage".equals(pageStr)) {pageIndex = lastPageIndex;}}if (pageIndex < 0) {pageIndex = 0;}if (pageIndex > lastPageIndex) {pageIndex = lastPageIndex;}//将html输出到浏览器上JRHtmlExporter exporter = new JRHtmlExporter();response.setCharacterEncoding("UTF-8");out = response.getWriter();//分页exporter.setParameter(JRExporterParameter.PAGE_INDEX, pageIndex);exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);exporter.exportReport();} catch (Exception e) {e.printStackTrace();}finally {out.flush();out.close();}}private JasperPrint getJasperPrint(String jasperPath, Map<String, Object> parameters, List<?> list) throws JRException {//解决大数据量保留在内存中,只在内存中保留两页,剩下的放入磁盘中JRFileVirtualizer virtualizer = new JRFileVirtualizer(2, "D:/");parameters.put(JRParameter.REPORT_VIRTUALIZER, virtualizer);virtualizer.setReadOnly(true);return JasperFillManager.fillReport(jasperPath, parameters, new JRBeanCollectionDataSource(list)); }protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}}
ModelTableSource.java
package com.jasper.bean;import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;public class ModelTableSource {/*** 注入table组件的数据源*/private JRBeanCollectionDataSource tableData;/*** 当前时间(查询时间)*/private String date;public JRBeanCollectionDataSource getTableData() {return tableData;}public void setTableData(JRBeanCollectionDataSource tableData) {this.tableData = tableData;}public String getDate() {return date;}public void setDate(String date) {this.date = date;}}
Users.java
package com.jasper.bean;public class Users {private Integer userId;private String userName;private String grade;private String college;private String mobile;public Integer getUserId() {return userId;}public void setUserId(Integer userId) {this.userId = userId;}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public String getGrade() {return grade;}public void setGrade(String grade) {this.grade = grade;}public String getCollege() {return college;}public void setCollege(String college) {this.college = college;}public String getMobile() {return mobile;}public void setMobile(String mobile) {this.mobile = mobile;}public Users(Integer userId, String userName, String grade, String college, String mobile) {super();this.userId = userId;this.userName = userName;this.grade = grade;this.college = college;this.mobile = mobile;}public Users() {super();}}
代码下载:https://download.csdn.net/download/u013456370/10588521
这篇关于Jasperreports+jaspersoft studio学习教程(八)- 报表分页和大量数据内存处理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!