iReport工具的使用(三)
Java后台代码的实现
申明:直接拷贝代码会有错误,这里只是为了说明流程。
前面一节中已经创建了DataBean,这一节主要来完成报表的填充。
1、 需要把所有报表中展现的数据封装成一个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) {
//这里通过连接数据库或者其它方式将报表中展现的数据封装为一个List
return List对象;
}
}
import java.util.List;
import direction.shoufei.report.groupyearfee.data.CenterYearFee;
public class CenterYearFeeModel {
public List<CenterYearFee> getCenterYearFeeList(String year) {
//这里通过连接数据库或者其它方式将报表中展现的数据封装为一个List
return List对象;
}
}
2、 生成数据源,必须实现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());
}
}
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、 填充报表
a-接口
package direction.shoufei.report.groupyearfee.service;
public interface CenterYearFeeService {
/**
* 填充报表
* @param year 某年的报表
*/
void fillCenterYearFeeToReport(String year);
}
public interface CenterYearFeeService {
/**
* 填充报表
* @param year 某年的报表
*/
void fillCenterYearFeeToReport(String year);
}
b-填充报表的实现
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设计好的报表编译之后的路径
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.");
//填充报表时向报表传递的参数
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 {
//这里将生成的数据源和参数填充到报表里
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();
}
}
}
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设计好的报表编译之后的路径
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.");
//填充报表时向报表传递的参数
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 {
//这里将生成的数据源和参数填充到报表里
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();
}
}
}
这样就完成了报表的填充,报表的展现将在下一节说明。