iReport工具的使用(三)
生活随笔
收集整理的這篇文章主要介紹了
iReport工具的使用(三)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Java后臺代碼的實(shí)現(xiàn)
申明:直接拷貝代碼會有錯(cuò)誤,這里只是為了說明流程。
前面一節(jié)中已經(jīng)創(chuàng)建了DataBean,這一節(jié)主要來完成報(bào)表的填充。
1、?需要把所有報(bào)表中展現(xiàn)的數(shù)據(jù)封裝成一個(gè)DataBean對象的List
package?direction.shoufei.report.groupyearfee.model;import?java.util.List;
import?direction.shoufei.report.groupyearfee.data.CenterYearFee;
public?class?CenterYearFeeModel?{
????public?List<CenterYearFee>?getCenterYearFeeList(String?year)?{
????????//這里通過連接數(shù)據(jù)庫或者其它方式將報(bào)表中展現(xiàn)的數(shù)據(jù)封裝為一個(gè)List
????????return?List對象;
????}
}
?
2、?? ?生成數(shù)據(jù)源,必須實(shí)現(xiàn)JRDataSource接口package?direction.shoufei.report.groupyearfee.datasource;import?java.util.ArrayList;
import?java.util.List;
import?net.sf.jasperreports.engine.JRDataSource;
import?net.sf.jasperreports.engine.JRException;
import?net.sf.jasperreports.engine.JRField;
import?direction.shoufei.report.groupyearfee.data.CenterYearFee;
import?direction.shoufei.report.groupyearfee.model.CenterYearFeeModel;
public?class?CenterYearFeeDataSource?implements?JRDataSource?{
????private?List<CenterYearFee>?data?=?new?ArrayList<CenterYearFee>();
????public?CenterYearFeeDataSource(String?year)?{
????????data?=?new?CenterYearFeeModel().getCenterYearFeeList(year);
????}
????private?int?index?=?-1;
????@Override
????public?Object?getFieldValue(JRField?field)?throws?JRException?{
????????Object?value?=?null;
????????String?fieldName?=?field.getName();
????????if?("centerId".equals(fieldName))?{
????????????value?=?data.get(index).getCenterId();
????????}?else?if?("centerName".equals(fieldName))?{
????????????value?=?data.get(index).getCenterName();
????????}?else?if?("summary".equals(fieldName))?{
????????????value?=?data.get(index).getSummary();
????????}?else?if?("preYearRate".equals(fieldName))?{
????????????value?=?data.get(index).getPreYearRate();
????????}?else?if?("month1Fee".equals(fieldName))?{
????????????value?=?data.get(index).getMonth1Fee();
????????}?else?if?("month2Fee".equals(fieldName))?{
????????????value?=?data.get(index).getMonth2Fee();
????????}?else?if?("month3Fee".equals(fieldName))?{
????????????value?=?data.get(index).getMonth3Fee();
????????}
????????...
????????return?value;
????}
????@Override
????public?boolean?next()?throws?JRException?{
????????index++;
????????return?(index?<?data.size());
????}
}
?
3、?填充報(bào)表
a-接口
package?direction.shoufei.report.groupyearfee.service;public?interface?CenterYearFeeService?{
????/**
?????*?填充報(bào)表
?????*?@param?year?某年的報(bào)表
?????*/
????void?fillCenterYearFeeToReport(String?year);
}
b-填充報(bào)表的實(shí)現(xiàn)
package?direction.shoufei.report.groupyearfee.service.support;import?java.io.File;
import?java.util.Date;
import?java.util.HashMap;
import?java.util.Map;
import?net.sf.jasperreports.engine.JRException;
import?net.sf.jasperreports.engine.JRRuntimeException;
import?net.sf.jasperreports.engine.JasperFillManager;
import?net.sf.jasperreports.engine.JasperPrint;
import?net.sf.jasperreports.j2ee.servlets.BaseHttpServlet;
import?direction.framework.utils.WebRootPath;
import?direction.shoufei.report.groupyearfee.datasource.CenterYearFeeDataSource;
import?direction.shoufei.report.groupyearfee.service.CenterYearFeeService;
import?direction.shoufei.report.util.DateFormatUtil;
import?flex.messaging.FlexContext;
public?class?CenterYearFeeServiceImpl?implements?CenterYearFeeService{
????@Override
????public?void?fillCenterYearFeeToReport(String?year)?{
????????//用iReport設(shè)計(jì)好的報(bào)表編譯之后的路徑
????????String?reportFileName?=?new?WebRootPath().getWebRootPath()
????????+?"/reports/shoufei/groupyearfee/GroupYearFee.jasper";
????????File?reportFile?=?new?File(reportFileName);
????????if?(!reportFile.exists())
????????????throw?new?JRRuntimeException(
????????????????????"File?GroupYearFee.jasper?not?found.?The?report?design?must?be?compiled?first.");
????????//填充報(bào)表時(shí)向報(bào)表傳遞的參數(shù)
????????Map?parameters?=?new?HashMap();
????????parameters.put("QueryYear",?year);
????????
????????String?endTimeStr?=?year+"-12-26";
????????Date?endTime?=?DateFormatUtil.getDateByStr(endTimeStr);
????????if(new?Date().getTime()>endTime.getTime()){
????????????endTimeStr?=?DateFormatUtil.getStrByDate(endTime);
????????}else{
????????????endTimeStr?=?DateFormatUtil.getStrByDate(new?Date());
????????}
????????
????????parameters.put("QueryEndTime",?endTimeStr);
????????parameters.put("PreYear",?DateFormatUtil.getPreYear(year));
????????
????????try?{
????????????//這里將生成的數(shù)據(jù)源和參數(shù)填充到報(bào)表里
????????????JasperPrint?jasperPrint?=?JasperFillManager.fillReport(
????????????????????reportFileName,?parameters,
????????????????????new?CenterYearFeeDataSource(year));
????????????FlexContext.getHttpRequest().getSession().setAttribute(
????????????????????BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE,
????????????????????jasperPrint);
????????}?catch?(JRException?e)?{
????????????e.printStackTrace();
????????}
????}
}
?
這樣就完成了報(bào)表的填充,報(bào)表的展現(xiàn)將在下一節(jié)說明。
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/wangtory/archive/2011/03/21/iReport3.html
總結(jié)
以上是生活随笔為你收集整理的iReport工具的使用(三)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 谈谈NullableT的类型转换问题
- 下一篇: 设置asp.net网站的信任等级