package com.xxxx.report.action;
import java.io.File;
import java.io.ObjectOutputStream;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.base.JRBaseReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JExcelApiExporter;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.export.JRXlsAbstractExporter;
import net.sf.jasperreports.engine.export.JRXlsAbstractExporterParameter;
import net.sf.jasperreports.engine.util.FileBufferedOutputStream;
import net.sf.jasperreports.engine.util.JRLoader;
import org.apache.struts2.ServletActionContext;
import com.xxxx.report.persistence.pojo.ReportDataBean;
import com.xxxx.report.service.IReportDataService;
/*
* 打印抽象类
*/
public abstract class ReportAction extends BaseAction implements IReport{
private static final long serialVersionUID = 1L;
private static String PARAM_SUB_REPORT_DIR="SUBREPORT_DIR";
private static String PARAM_EXCEL_EXPORT="EXCEL_EXPORT";
private static String VALUE_EXCEL_EXPORT_ENABLE="1";
private static String VALUE_EXCEL_EXPORT_DISABLE="0";
private IReportDataService reportDataService;
private List<ReportDataBean> dataSource;
/**
* 打印预览
* @return
*/
public String printPreView(){
try{
String reportPath =
ServletActionContext.getRequest().getRealPath(this.getJasperFile());
File reportFile = new File(reportPath);
JasperReport jasperReport = (JasperReport)JRLoader.loadObject(reportFile);
List<ReportDataBean> reportDataSource = this.getReportDataBean();
Map<String,Object> paramMap = this.getJasperPrintParam();
if(this.hasSubReport()==true){
paramMap.put(ReportAction.PARAM_SUB_REPORT_DIR, reportFile.getParent()+"/");
}
paramMap.put(ReportAction.PARAM_EXCEL_EXPORT,ReportAction.VALUE_EXCEL_EXPORT_DISABLE);
JasperPrint jasperPrint=JasperFillManager.fillReport(
jasperReport,paramMap, new JRBeanCollectionDataSource(reportDataSource));
//System.out.println("Date fill end:"+new Date());
/*通过反射改变页边距
java.lang.reflect.Field margin = JRBaseReport.class.getDeclaredField("leftMargin");
margin.setAccessible(true);
margin.setInt(jasperReport, 0);
*/
//System.out.println("Date show start:"+new Date());
if (jasperPrint != null)
{
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("application/octet-stream");
ServletOutputStream ouputStream = response.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(ouputStream);
oos.writeObject(jasperPrint);
oos.flush();
oos.close();
ouputStream.flush();
ouputStream.close();
}
//System.out.println("Date show end:"+new Date());
}catch(Exception e){
e.printStackTrace();
}
return null;
}
/**
* 导出XLS
* @return
*/
public String exportXLS(){
FileBufferedOutputStream fbos = null;
ServletOutputStream ouputStream = null;
try{
String reportPath =
ServletActionContext.getRequest().getRealPath(this.getJasperFile());
File reportFile = new File(reportPath);
JasperReport jasperReport = (JasperReport)JRLoader.loadObject(reportFile);
List<ReportDataBean> reportDataSource = this.getReportDataBean();
Map<String,Object> paramMap = this.getJasperPrintParam();
if(this.hasSubReport()){
paramMap.put(ReportAction.PARAM_SUB_REPORT_DIR, reportFile.getParent()+"/");
}
paramMap.put(ReportAction.PARAM_EXCEL_EXPORT,ReportAction.VALUE_EXCEL_EXPORT_ENABLE);
//通过反射改变页边距
//kill left margin
java.lang.reflect.Field margin = JRBaseReport.class.getDeclaredField("leftMargin");
margin.setAccessible(true);
margin.setInt(jasperReport, 0);
//kill top margin
margin = JRBaseReport.class.getDeclaredField("topMargin");
margin.setAccessible(true);
margin.setInt(jasperReport, 0);
JasperPrint jasperPrint=JasperFillManager.fillReport(
jasperReport,paramMap, new JRBeanCollectionDataSource(reportDataSource));
if(null != jasperPrint){
fbos = new FileBufferedOutputStream();
JRXlsAbstractExporter exporter = new JExcelApiExporter();
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, fbos);
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRXlsAbstractExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
exporter.setParameter(JRXlsAbstractExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
exporter.setParameter(JRXlsAbstractExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
exporter.setParameter(JRXlsAbstractExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);
exporter.exportReport();
fbos.close();
if(fbos.size()>0) {
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("application/exl");
//TODO
response.setHeader("Content-Disposition", "attachment;filename=\"target.xls\"");
response.setContentLength(fbos.size());
ouputStream = response.getOutputStream();
fbos.writeData(ouputStream);
fbos.dispose();
ouputStream.flush();
}
}
}catch (Exception e1) {
e1.printStackTrace();
}finally{
try{
if(null != ouputStream) {
ouputStream.close();
}
if(null !=fbos){
fbos.close();
fbos.dispose();
}
}catch(Exception e2){
e2.printStackTrace();
}
}
return null;
}
/**
* 导出PDF
* @return
*/
public String exportPDF(){
FileBufferedOutputStream fbos = null;
ServletOutputStream ouputStream = null;
try{
String reportPath =
ServletActionContext.getRequest().getRealPath(this.getJasperFile());
File reportFile = new File(reportPath);
JasperReport jasperReport = (JasperReport)JRLoader.loadObject(reportFile);
List<ReportDataBean> reportDataSource = this.getReportDataBean();
Map<String,Object> paramMap = this.getJasperPrintParam();
if(this.hasSubReport()==true){
paramMap.put(ReportAction.PARAM_SUB_REPORT_DIR, reportFile.getParent()+"/");
}
//System.out.println("Date fill start:"+new Date());
JasperPrint jasperPrint=JasperFillManager.fillReport(
jasperReport,paramMap, new JRBeanCollectionDataSource(reportDataSource));
if(null != jasperPrint){
fbos = new FileBufferedOutputStream();
JRPdfExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, fbos);
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.exportReport();
fbos.close();
if(fbos.size()>0) {
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("application/pdf");
//TODO
response.setHeader("Content-Disposition", "attachment;filename=\"target.pdf\"");
re
没有合适的资源?快使用搜索试试~ 我知道了~
SSH集成JasperReport例子,支持预览导出Exce导出Pdf
共91个文件
jar:24个
class:13个
java:13个
4星 · 超过85%的资源 需积分: 34 239 下载量 196 浏览量
2012-02-22
13:01:30
上传
评论 4
收藏 14.11MB ZIP 举报
温馨提示
一个使用SSH集成JasperReport的例子,支持预览、导出Excel、导出Pdf。 部署到Tomcat服务器可运行,该例子中采用了一个报表框架,框架的类图结构可参考压缩包中的“报表设计.jpg”。 希望对您有帮助。
资源推荐
资源详情
资源评论
收起资源包目录
JasperReportSample.zip (91个子文件)
TestJasper
.project 1KB
WebContent
report
jasperxml
bid_project.jrxml 13KB
bid_project_sub_bid.jrxml 2KB
bid_project.jsp 3KB
jasper
bid_project_sub_bid.jasper 25KB
images
px 807B
bid_project.jasper 28KB
applets
EmbeddedViewerApplet.java 3KB
JRViewerSimple.class 674B
JRViewerSimple.java 2KB
EmbeddedViewerApplet.class 2KB
jasperreports-applet-3.7.0.jar 354KB
commons-logging-1.0.4.jar 37KB
commons-collections-2.1.1.jar 171KB
view.jsp 1KB
WEB-INF
lib
javassist-3.9.0.GA.jar 583KB
commons-io-1.3.1.jar 82KB
ognl-2.6.11.jar 164KB
spring.jar 2.81MB
dom4j-1.6.1.jar 307KB
struts2-jasperreports-plugin-2.0.14.jar 16KB
log4j-1.2.14.jar 359KB
commons-beanutils-1.8.0.jar 226KB
standard-1.1.2.jar 384KB
struts2-spring-plugin-2.0.14.jar 9KB
commons-digester-1.7.jar 137KB
jasperreports-3.7.0.jar 2.51MB
jsonplugin-0.32.jar 45KB
commons-collections-3.1.jar 546KB
struts2-core-2.0.14.jar 2.22MB
groovy-all-1.5.5.jar 2.71MB
xwork-2.0.7.jar 449KB
common-annotations.jar 6KB
poi-3.2-FINAL-20081019.jar 1.36MB
commons-logging-1.0.4.jar 37KB
jstl-1.1.2.jar 20KB
web.xml 1KB
index.html 312B
js
.svn
entries 1KB
text-base
jquery.treeview.js.svn-base 8KB
common.js.svn-base 1KB
left.js.svn-base 2KB
jquery.cookie.js.svn-base 4KB
bgiframe.js.svn-base 1KB
weebox.js.svn-base 15KB
upload.js.svn-base 227B
jquery.js.svn-base 56KB
jquery-1.2.6.pack.js 30KB
jquery.js 56KB
weebox.js 16KB
common.js 5KB
bgiframe.js 1KB
META-INF
MANIFEST.MF 39B
src
struts.xml 1KB
com
xxxx
report
service
IReportDataService.java 234B
impl
BidProjectReportDataService.java 829B
action
BaseAction.java 1014B
ReportPrintManagerAction.java 759B
BidProjectReportAction.java 1KB
ReportAction.java 9KB
IReport.java 550B
persistence
pojo
ReportDataBean.java 91B
BidProjectSubDataBean.java 1KB
BidProjectQuery.java 434B
BidProjectDataBean.java 4KB
log4j.properties 1KB
struts.properties 261B
spring.xml 992B
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 338B
org.eclipse.jdt.core.prefs 395B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.component 460B
.jsdtscope 503B
build
classes
struts.xml 1KB
com
xxxx
report
service
impl
BidProjectReportDataService.class 2KB
IReportDataService.class 314B
action
IReport.class 459B
BidProjectReportAction.class 2KB
BaseAction.class 1KB
ReportPrintManagerAction.class 1KB
ReportAction.class 8KB
persistence
pojo
BidProjectSubDataBean.class 1KB
BidProjectDataBean.class 5KB
BidProjectQuery.class 792B
ReportDataBean.class 333B
log4j.properties 1KB
struts.properties 261B
spring.xml 992B
.classpath 834B
报表设计.JPG 81KB
共 91 条
- 1
xwyzl2
- 粉丝: 2
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页