您的位置:首页 > 编程语言 > ASP

struts2整合jasperreport打印多张报表

2017-09-20 10:34 411 查看
用javabean做的报表数据源,model中的list做子报表的数据源

public class Model020104 extends ExamCourseEntity implements Serializable{

/**
*
*/
private static final long serialVersionUID = -8689179816540911881L;
/** 考试id */
private String exam_id;
/** 考试批次 */
private String test_name;
/** 学习中心id */
private String xxzx_id;
/** 学习中心 */
private String xxzx;
/** 层次 */
private String cengci;
/** 专业 */
private String major_name;
/** 年级 */
private String grade;
/** 课程名称 */
private String kcmc;

/** 考点id */
private String examination_area_id;
/** 考点名称 */
private String examination_area_name;

/** 考场id */
private String examination_room_id;

/** 考场名称 */
private String examination_room_name;
/** 应考人数 */
private Integer yingkao_num;
/** 学生列表 */
private List<MStudentEntity> stuList;
//private JRBeanCollectionDataSource stuList;
private JRBeanCollectionDataSource jrs;

private String searchcondition;
/** 检索更多标识 */
private String moreFlag = "0";
public String getExam_id() {
return exam_id;
}

public void setExam_id(String exam_id) {
this.exam_id = exam_id;
}

public String getTest_name() {
return test_name;
}

public void setTest_name(String test_name) {
this.test_name = test_name;
}

public String getXxzx() {
return xxzx;
}

public void setXxzx(String xxzx) {
this.xxzx = xxzx;
}

public String getCengci() {
return cengci;
}

public void setCengci(String cengci) {
this.cengci = cengci;
}

public String getKcmc() {
return kcmc;
}

public void setKcmc(String kcmc) {
this.kcmc = kcmc;
}

public String getExamination_area_name() {
return examination_area_name;
}

public void setExamination_area_name(String examination_area_name) {
this.examination_area_name = examination_area_name;
}

public String getExamination_room_name() {
return examination_room_name;
}

public void setExamination_room_name(String examination_room_name) {
this.examination_room_name = examination_room_name;
}

public String getXxzx_id() {
return xxzx_id;
}

public void setXxzx_id(String xxzx_id) {
this.xxzx_id = xxzx_id;
}

public String getExamination_area_id() {
return examination_area_id;
}

public void setExamination_area_id(String examination_area_id) {
this.examination_area_id = examination_area_id;
}

public String getExamination_room_id() {
return examination_room_id;
}

public void setExamination_room_id(String examination_room_id) {
this.examination_room_id = examination_room_id;
}

public String getMajor_name() {
return major_name;
}

public void setMajor_name(String major_name) {
this.major_name = major_name;
}

public String getGrade() {
return grade;
}

public void setGrade(String grade) {
this.grade = grade;
}

public Integer getYingkao_num() {
return yingkao_num;
}

public void setYingkao_num(Integer yingkao_num) {
this.yingkao_num = yingkao_num;
}

public JRBeanCollectionDataSource getJrs() {
return jrs;
}

public void setJrs(JRBeanCollectionDataSource jrs) {
this.jrs = jrs;
}

public String getSearchcondition() {
return searchcondition;
}

public void setSearchcondition(String searchcondition) {
this.searchcondition = searchcondition;
}

public String getMoreFlag() {
return moreFlag;
}

public void setMoreFlag(Stri
4000
ng moreFlag) {
this.moreFlag = moreFlag;
}

public List<MStudentEntity> getStuList() {
return stuList;
}

public void setStuList(List<MStudentEntity> stuList) {
this.stuList = stuList;
}

}

action中的方法
public class ReportAction extends BaseAction implements ModelDriven<Model020104> {

/**
*
*/
private static final long serialVersionUID = -1641985675495601650L;
private static LogHelper log = new LogHelper(ReportAction.class);

  private Model020104 model;
private String searchflg;
private Model020105 modelStu = new Model020105();
private ExamDaoImpl eImpl = new ExamDaoImpl();
ExamCourseDaoImpl ecImpl = new ExamCourseDaoImpl();
MStudentDaoImpl msImpl = new MStudentDaoImpl();
List<Model020104> mlist = new ArrayList<Model020104>();
public Map<String, Object> map = new HashMap<String, Object>();
public Map<String, Object> map2 = new HashMap<String, Object>();
Service020104 serv = new Service020104();
private SortedMap[] smap;
//public Map<String, Object> reportParameter = new HashMap<String, Object>();
private SortedMap[] testMap;
private String pdfName;
private InputStream fileInputStream;
public Map<Object, Object> exportParameter = new HashMap<Object, Object>();
private final static Map params = new HashMap();

@Override
public Model020104 getModel() {
if (model == null) {
model = new Model020104();
}
return model;
}

@Override
public String execute() throws Exception {
try {
if (null == model.getExam_id()) {
Result rst = eImpl.getExamResultOrder();
model.setExam_id((String) rst.getRows()[0].get("exam_id"));
}
Integer countdata = ecImpl.getExamInfoCount(model);
setPageCount((int) Math.ceil((double) countdata / getRowCount()));
Result rs = ecImpl.getExamInfo(model, getPageIndex() * getRowCount(), (getPageIndex() + 1) * getRowCount());
smap = rs.getRows();
// 设置页面的考试批次
Result rsExam = eImpl.getExamList();
testMap = rsExam.getRows();
} catch (Exception e) {
HttpSession session = ServletActionContext.getRequest().getSession();
session.setAttribute(BaseSymbols.SESSION_KEY_EXCEPTION, e);
log.error(BaseSymbols.EXCEPTION_MSG_BUSINESS, e);
return BaseSymbols.Struts_Request_Error;
}
return BaseSymbols.Struts_Request_Success;
}

/**
* 导出到服务器,下载
**/
@SuppressWarnings("unchecked")
public String print() throws Exception {
try {
mlist = serv.getExamInfoList(model);
pdfName = new String(
(mlist.get(0).getTest_name() + mlist.get(0).getKcmc() + mlist.get(0).getExamination_area_name()
+ mlist.get(0).getExamination_room_name() + ".pdf").getBytes(),
"ISO8859-1");
modelStu.setExamination_area_id(model.getExamination_area_id());
modelStu.setExamination_room_id(model.getExamination_room_id());
modelStu.setExam_course_id(model.getExam_course_id());
List<Model020105> mdList = msImpl.getStudentListByExam(modelStu);
JasperReportUtil.exportToPdf(mlist, mdList, map, pdfName);
fileInputStream = new FileInputStream(new File(BaseSymbols.ROOT_PATH + "reports/" + pdfName));
} catch (Exception e) {
HttpSession session = ServletActionContext.getRequest().getSession();
session.setAttribute(BaseSymbols.SESSION_KEY_EXCEPTION, e);
log.error(BaseSymbols.EXCEPTION_MSG_BUSINESS, e);
return BaseSymbols.Struts_Request_Error;
}
return BaseSymbols.Struts_Request_Success;

}

/**
* struts2,pdf预览打印(用这种方法)
* */
public String previewAndPrint() throws Exception{
try {
//获得主报表的数据源mlist
mlist = ecImpl.getExamSignIn(model);
for(int i=0;i<mlist.size();i++){
mlist.get(i).setYingkao_num(msImpl.getYingkaoCount(model));
}
//子报表的数据源stuList
List<MStudentEntity> stuList = msImpl.getMStudentListByCondition(model);
//配置reportParameter单张报表时是可以的
 //reportParameter.put("stuList", stuList);
//导出pdf的文件名
pdfName =new String((mlist.get(0).getTest_name()+mlist.get(0).getKcmc()+mlist.get(0).getExamination_area_name()+mlist.get(0).getExamination_room_name()).getBytes(),"ISO8859-1");
//获得拼接的报表文件的数据源
modelStu.setExamination_area_id(model.getExamination_area_id());
modelStu.setExamination_room_id(model.getExamination_room_id());
modelStu.setExam_course_id(model.getExam_course_id());
List<Model020105> mdList = msImpl.getStudentListByExam(modelStu);
//主报表需要的参数
map.put("stuList", stuList);
//主报表
 JasperPrint jp1 = JasperFillManager.fillReport(BaseSymbols.ROOT_PATH+"reports/MSignRP.jasper", map, new JRBeanCollectionDataSource(mlist));
//拼接的报表
JasperPrint jp2 = JasperFillManager.fillReport(BaseSymbols.ROOT_PATH+"reports/examZFRP.jasper", map2, new JRBeanCollectionDataSource(mdList));
List<JasperPrint> jpList = new ArrayList<JasperPrint>();
jpList.add(jp1);
jpList.add(jp2);
//struts.xml需要的exportParameters参数
exportParameter.put(JRExporterParameter.JASPER_PRINT_LIST, jpList);
} catch (Exception e) {
HttpSession session = ServletActionContext.getRequest().getSession();
session.setAttribute(BaseSymbols.SESSION_KEY_EXCEPTION, e);
log.error(BaseSymbols.EXCEPTION_MSG_BUSINESS, e);
return BaseSymbols.Struts_Request_Error;
}
return BaseSymbols.Struts_Request_Success;
}

public List<Model020104> getMlist() {
return mlist;
}

public void setMlist(List<Model020104> mlist) {
this.mlist = mlist;
}

public Map<String, Object> getMap() {
return map;
}

public void setMap(Map<String, Object> map) {
this.map = map;
}

public SortedMap[] getSmap() {
return smap;
}

public void setSmap(SortedMap[] smap) {
this.smap = smap;
}

// public Map<String, Object> getReportParameter() {
// return reportParameter;
// }
//
// public void setReportParameter(Map<String, Object> reportParameter) {
// this.reportParameter = reportParameter;
// }

public SortedMap[] getTestMap() {
return testMap;
}

public void setTestMap(SortedMap[] testMap) {
this.testMap = testMap;
}

public String getSearchflg() {
return searchflg;
}

public void setSearchflg(String searchflg) {
this.searchflg = searchflg;
}

public String getPdfName() {
return pdfName;
}

public void setPdfName(String pdfName) {
this.pdfName = pdfName;
}

public Map<Object, Object> getExportParameter() {
return exportParameter;
}

public void setExportParameter(Map<Object, Object> exportParameter) {
this.exportParameter = exportParameter;
}

public InputStream getFileInputStream() {
return fileInputStream;
}

}然后是struts.xml中的配置
<package name="c" extends="struts-default,json-default,jasperreports-default">
<action name="ReportAction_PDF" class="com.xxxx.action.ReportAction" method="previewAndPrint">
<result name="Success" type="jasper">
<param name="location">/reports/MSignRP.jasper</param>
<param name="dataSource">mlist</param>
<!-- 多张报表就不用这个参数了,单张的用这个可以 -->
 <!-- <param name="reportParameters">reportParameter</param> -->
<param name="exportParameters">exportParameter</param>
<param name="documentName">${pdfName}</param>
<param name="format">PDF</param>
</result>
</action>
</package>

报表文件没什么,就是在主报表的参数里添加个stuList的参数,在主报表上的subReport组件设置Data Source Expression为new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{stuList})
忙活了好几天,参照了不少大神的博客,终于把看到的坑都填完了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: