Ireport 报表导出 Poi + ireport 导出pdf, word ,excel ,html 格式

Ireport报表导出Poi+ireport导出pdf,doc,excel,html格式

下面是报表导出工具类reportExportUtils需要导出以上格式的报表只需要调用本工具类的exportmain方法即可。

importjava.io.File;

importjava.io.FileInputStream;

importjava.io.FileNotFoundException;

importjava.io.IOException;

importjava.io.InputStream;

importjava.lang.reflect.Field;

importjava.util.Collection;

importjava.util.HashMap;

importjava.util.List;

importjava.util.Map;

importjavax.servlet.ServletOutputStream;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

importnet.sf.jasperreports.engine.JRDataSource;

importnet.sf.jasperreports.engine.JRException;

importnet.sf.jasperreports.engine.JRExporter;

importnet.sf.jasperreports.engine.JRExporterParameter;

importnet.sf.jasperreports.engine.JasperExportManager;

importnet.sf.jasperreports.engine.JasperFillManager;

importnet.sf.jasperreports.engine.JasperPrint;

importnet.sf.jasperreports.engine.JasperReport;

importnet.sf.jasperreports.engine.base.JRBaseReport;

importnet.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

importnet.sf.jasperreports.engine.export.JRHtmlExporter;

importnet.sf.jasperreports.engine.export.JRHtmlExporterParameter;

importnet.sf.jasperreports.engine.export.JRRtfExporter;

importnet.sf.jasperreports.engine.export.JRXlsExporter;

importnet.sf.jasperreports.engine.export.JRXlsExporterParameter;

importnet.sf.jasperreports.engine.util.JRLoader;

importorg.apache.commons.lang.StringUtils;

importorg.apache.log4j.Logger;

importorg.apache.struts2.ServletActionContext;

importcom.opensymphony.xwork2.ActionContext;

@SuppressWarnings("unchecked")

publicclassJasperUtils{

privatestaticLoggerlogger=Logger.getLogger(JasperUtils.class);

publicstaticfinalStringPRINT_TYPE="print";

publicstaticfinalStringPDF_TYPE="pdf";

publicstaticfinalStringEXCEL_TYPE="excel";

publicstaticfinalStringHTML_TYPE="html";

publicstaticfinalStringWORD_TYPE="word";

publicstaticvoidprepareReport(JasperReportjasperReport,Stringtype){

logger

.debug("Themethod=======prepareReport()start.......................");

/*

*如果导出的是excel,则需要去掉周围的margin

*/

if("excel".equals(type))

try{

Fieldmargin=JRBaseReport.class

.getDeclaredField("leftMargin");

margin.setAccessible(true);

margin.setInt(jasperReport,0);

margin=JRBaseReport.class.getDeclaredField("topMargin");

margin.setAccessible(true);

margin.setInt(jasperReport,0);

margin=JRBaseReport.class.getDeclaredField("bottomMargin");

margin.setAccessible(true);

margin.setInt(jasperReport,0);

FieldpageHeight=JRBaseReport.class

.getDeclaredField("pageHeight");

pageHeight.setAccessible(true);

pageHeight.setInt(jasperReport,2147483647);

}catch(Exceptionexception){

}

}

/**

*导出excel

*/

publicstaticvoidexportExcel(JasperPrintjasperPrint,

StringdefaultFilename,HttpServletRequestrequest,

HttpServletResponseresponse)throwsIOException,JRException{

logger

.debug("执行导出excelThemethod=======exportExcel()start.......................");

/*

*设置头信息

*/

response.setContentType("application/vnd.ms-excel");

Stringdefaultname=null;

if(defaultFilename.trim()!=null&&defaultFilename!=null){

defaultname=defaultFilename+".xls";

}else{

defaultname="export.xls";

}

StringfileName=newString(defaultname.getBytes("gbk"),"utf-8");

response.setHeader("Content-disposition","attachment;filename="

+fileName);

ServletOutputStreamouputStream=response.getOutputStream();

JRXlsExporterexporter=newJRXlsExporter();

exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);

exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,ouputStream);

exporter.setParameter(

JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,

Boolean.TRUE);

exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,

Boolean.FALSE);

exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,

Boolean.FALSE);

exporter.exportReport();

ouputStream.flush();

ouputStream.close();

}

/**

*导出pdf,注意此处中文问题,

*

*这里应该详细说:主要在ireport里变下就行了。看图

*

*1)在ireportclasspath中加入iTextAsian.jar2)在ireportjrxml时,看ireport最左边有个属性栏。

*

*下边的设置就在点字段的属性后出现。pdffontnameSTSong-LightpdfencodingUniGB-UCS2-H

*/

privatestaticvoidexportPdf(JasperPrintjasperPrint,

StringdefaultFilename,HttpServletRequestrequest,

HttpServletResponseresponse)throwsIOException,JRException{

response.setContentType("application/pdf");

Stringdefaultname=null;

if(defaultFilename.trim()!=null&&defaultFilename!=null){

defaultname=defaultFilename+".pdf";

}else{

defaultname="export.pdf";

}

StringfileName=newString(defaultname.getBytes("GBK"),"utf-8");

response.setHeader("Content-disposition","attachment;filename="

+fileName);

ServletOutputStreamouputStream=response.getOutputStream();

JasperExportManager.exportReportToPdfStream(jasperPrint,ouputStream);

ouputStream.flush();

ouputStream.close();

}

/**

*导出html

*

*@parampageIndexStr

*/

privatestaticvoidexportHtml(JasperPrintjasperPrint,

StringdefaultFilename,HttpServletRequestrequest,

HttpServletResponseresponse,StringpageIndexStr)

throwsIOException,JRException{

response.setContentType("text/html");

ServletOutputStreamouputStream=response.getOutputStream();

JRHtmlExporterexporter=newJRHtmlExporter();

//得到当前页码

IntegerpageIndex=getPageIndex(jasperPrint,request,pageIndexStr);

//如果页码不为空则设置分页页码

if(pageIndex!=null){

exporter.setParameter(JRExporterParameter.PAGE_INDEX,pageIndex);

}

exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,

Boolean.FALSE);

exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);

exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING,"UTF-8");

exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,ouputStream);

exporter.exportReport();

ouputStream.flush();

ouputStream.close();

}

/**

*

*description:通过传进来的pageIndexStr得到当前页码Date2013-1-18上午10:49:38

*

*@param@paramjasperPrint

*@param@paramrequest

*@param@parampageIndexStr

*@param@return

*@returnInteger

*/

privatestaticIntegergetPageIndex(JasperPrintjasperPrint,

HttpServletRequestrequest,StringpageIndexStr){

//如果pageIndexStr为空或空字符串则返回null

if(pageIndexStr==null||StringUtils.isBlank(pageIndexStr)){

returnnull;

}

IntegerpageIndex=0;

intlastPageIndex=0;

//得到最后一页的页码

if(jasperPrint.getPages()!=null){

lastPageIndex=jasperPrint.getPages().size()-1;

}

//如果字符串==lastPage则反lastPageIndex的值赋给pageIndex并返回pageIndex

if("lastPage".equals(pageIndexStr)){

pageIndex=lastPageIndex;

returnpageIndex;

}

try{

pageIndex=Integer.parseInt(pageIndexStr);

//ireport传来的PageIndex是从1开始,而JRExporterParameter.PAGE_INDEX是从0开始的

if(pageIndex>0){

pageIndex=pageIndex-1;

}

}catch(Exceptione){

e.printStackTrace();

}

if(pageIndex<0){

pageIndex=0;

}

if(pageIndex>lastPageIndex){

pageIndex=lastPageIndex;

}

returnpageIndex;

}

/**

*导出word

*/

privatestaticvoidexportWord(JasperPrintjasperPrint,

StringdefaultFilename,HttpServletRequestrequest,

HttpServletResponseresponse)throwsJRException,IOException{

response.setContentType("application/msword;charset=utf-8");

Stringdefaultname=null;

if(defaultFilename.trim()!=null&&defaultFilename!=null){

defaultname=defaultFilename+".doc";

}else{

defaultname="export.doc";

}

StringfileName=newString(defaultname.getBytes("GBK"),"utf-8");

response.setHeader("Content-disposition","attachment;filename="

+fileName);

JRExporterexporter=newJRRtfExporter();

exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);

exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,response

.getOutputStream());

exporter.exportReport();

}

/**

*按照类型导出不同格式文件

*

*@paramdatas

*数据

*@paramtype

*文件类型

*@paramis

*jasper文件的来源

*@paramrequest

*@paramresponse

*@paramdefaultFilename默认的导出文件的名称

*/

privatestaticvoidexport(Collectiondatas,Stringtype,

StringdefaultFilename,InputStreamis,HttpServletRequestrequest,

HttpServletResponseresponse,StringpageIndexStr){

logger

.debug("导出判断Themethod=======export()start.......................");

try{

logger.info("is=="+is);

JasperReportjasperReport=(JasperReport)JRLoader.loadObject(is);

prepareReport(jasperReport,type);

JRDataSourceds=newJRBeanCollectionDataSource(datas,false);

Mapparameters=newHashMap();

JasperPrintjasperPrint=JasperFillManager.fillReport(

jasperReport,parameters,ds);

if(EXCEL_TYPE.equals(type)){

exportExcel(jasperPrint,defaultFilename,request,response);

}elseif(PDF_TYPE.equals(type)){

exportPdf(jasperPrint,defaultFilename,request,response);

}elseif(HTML_TYPE.equals(type)){

exportHtml(jasperPrint,defaultFilename,request,response,

pageIndexStr);

}elseif(WORD_TYPE.equals(type)){

exportWord(jasperPrint,defaultFilename,request,response);

}

}catch(Exceptione){

e.printStackTrace();

}

}

/**

*导出入口(导出所有的数据)

*

*@paramexportType

*导出文件的类型

*@paramjaspername

*jasper文件的名字如:xx.jasper

*@paramlists

*导出的数据

*@paramrequest

*@paramresponse

*@paramdefaultFilename默认的导出文件的名称

*/

publicstaticvoidexportmain(StringexportType,Stringjaspername,

Listlists,StringdefaultFilename){

exportmain(exportType,jaspername,lists,defaultFilename,null);

}

/**

*导出入口(分页显示导出传入的pageIndex页的数据)

*

*@paramexportType

*导出文件的类型

*@paramjaspername

*jasper文件的名字如:xx.jasper

*@paramlists

*导出的数据

*@paramrequest

*@paramresponse

*@paramdefaultFilename默认的导出文件的名称

*@parampageIndex

*需要导出数据的页码当pageIndex=null时导出所有数据

*/

@SuppressWarnings("deprecation")

publicstaticvoidexportmain(StringexportType,Stringjaspername,

Listlists,StringdefaultFilename,StringpageIndexStr){

logger

.debug("进入导出Themethod=======exportmain()start.......................");

ActionContextct=ActionContext.getContext();

HttpServletRequestrequest=(HttpServletRequest)ct

.get(ServletActionContext.HTTP_REQUEST);

HttpServletResponseresponse=ServletActionContext.getResponse();

Stringfilenurl=request.getRealPath("/view/situation/jasper/"

+jaspername);

Filefile=newFile(filenurl);

InputStreamis=null;

try{

is=newFileInputStream(file);

}catch(FileNotFoundExceptione){

e.printStackTrace();

}

export(lists,exportType,defaultFilename,is,request,response,

pageIndexStr);

}

}

你可能感兴趣的:(IREPORT)