java ireport报表,Java IReport报表工具类

packagecom.report

importcom.status.ReportExportMode

importnet.sf.jasperreports.engine.JRDataSource

importnet.sf.jasperreports.engine.JRException

importnet.sf.jasperreports.engine.JRExporter

importnet.sf.jasperreports.engine.JRExporterParameter

importnet.sf.jasperreports.engine.JasperCompileManager

importnet.sf.jasperreports.engine.JasperFillManager

importnet.sf.jasperreports.engine.JasperPrint

importnet.sf.jasperreports.engine.JasperPrintManager

importnet.sf.jasperreports.engine.JasperReport

importnet.sf.jasperreports.engine.data.JRBeanCollectionDataSource

importnet.sf.jasperreports.engine.export.JRHtmlExporter

importnet.sf.jasperreports.engine.export.JRHtmlExporterParameter

importnet.sf.jasperreports.engine.export.JRPdfExporter

importnet.sf.jasperreports.engine.export.JRRtfExporter

importnet.sf.jasperreports.engine.export.JRXlsExporter

importnet.sf.jasperreports.engine.export.JRXlsExporterParameter

importnet.sf.jasperreports.engine.util.JRLoader

importjavax.servlet.ServletContext

importjavax.servlet.ServletOutputStream

importjavax.servlet.http.HttpServletRequest

importjavax.servlet.http.HttpServletResponse

classExpeortReportService {

/***获取打印报表*@paramrequest*@paramresponse*@paramreportId模板名称,不带后缀*@paramexportMode导出格式:PDF/EXCEL/RTF/WORD/HTML*@paramparameterMap map数据*@paramdataList模板集合数据*@paramdownloadFileName下载的文件名*@throwsException*/public static voidexportReport(HttpServletRequest request, HttpServletResponse response, String reportId,

String exportMode, Map parameterMap, List dataList, String downloadFileName) throwsException {

try{

ServletContext servletContext = request.getSession().getServletContext()

File jasperFile = newFile(servletContext.getRealPath(File.separator+"templates"+File.separator+"jasper"+ File.separator+ reportId + ".jasper"))

if(!jasperFile.exists()) {

String tempPath = servletContext.getRealPath(File.separator+"templates"+File.separator+"jrxml"+File.separator+ reportId+".jrxml")

//编译后的.jasper文件存放路径String jrxmlDestSourcePath = servletContext.getRealPath(File.separator+"templates")+File.separator+"jasper"+File.separator+reportId+".jasper"JasperCompileManager.compileReportToFile(tempPath,jrxmlDestSourcePath)

}

if(parameterMap == null) {

parameterMap = newHashMap()

}

JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperFile)

JasperPrint jasperPrint = nullJRDataSource source = newJRBeanCollectionDataSource(dataList)

jasperPrint = JasperFillManager.fillReport(jasperReport, parameterMap, source)

if(request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0)

downloadFileName = newString(downloadFileName.getBytes("UTF-8"), "ISO8859-1")// firefox浏览器else if(request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0)

downloadFileName = newString(downloadFileName.getBytes("gb2312"), "ISO8859-1")// IE浏览器elsedownloadFileName = newString(downloadFileName.getBytes("UTF-8"), "ISO8859-1")// firefox浏览器if(ReportExportMode.EXP_PDF_MODE.equalsIgnoreCase(exportMode)) {

exportPdf(response, jasperPrint, downloadFileName)

} else if(ReportExportMode.EXP_EXCEL_MODE.equalsIgnoreCase(exportMode)) {

exportExcel(response, jasperPrint, downloadFileName)

} else if("WORD".equals(exportMode)) {

exportWord(response, jasperPrint, downloadFileName)

} else if("RTF".equals(exportMode)) {

exportRTF(response, jasperPrint, downloadFileName)

} else if("HTML".equals(exportMode)) {

exportHtml(response, jasperPrint, downloadFileName)

}

} finally{

}

}

/*** pdf导出*/private static voidexportPdf(HttpServletResponse response, JasperPrint jasperPrint, String downloadFileName)

throwsJRException, IOException {

ServletOutputStream ouputStream = response.getOutputStream()

try{

JRPdfExporter exporter = newJRPdfExporter()

exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint)

exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream)

//屏蔽copy功能// exporter.setParameter(JRPdfExporterParameter.,Boolean.TRUE)response.setContentType("application/pdf;charset=utf-8")

response.setHeader("Content-Disposition", "attachment;filename="+ downloadFileName + ".pdf")

exporter.exportReport()

ouputStream.flush()

} finally{

try{

ouputStream.close()

} catch(Exception e) {

}

}

}

/*** excel导出*/private static voidexportExcel(HttpServletResponse response, JasperPrint jasperPrint, String downloadFileName)

throwsJRException, IOException {

ServletOutputStream ouputStream = response.getOutputStream()

try{

JRXlsExporter exporter = newJRXlsExporter()

exporter.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint)

exporter.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, ouputStream)

response.setContentType("application/vnd.ms-excel;charset=utf-8")

response.setHeader("Content-Disposition", "attachment;filename="+ downloadFileName + ".xls")

//删除记录最下面的空行exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,Boolean.TRUE)

//删除多余的ColumnHeaderexporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,Boolean.FALSE)

//禁用白色背景exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,Boolean.FALSE)

exporter.exportReport()

ouputStream.flush()

} finally{

try{

ouputStream.close()

} catch(Exception e) {

}

}

}

/***导出word*/private static voidexportWord(HttpServletResponse response, JasperPrint jasperPrint, String downloadFileName)

throwsJRException, IOException {

ServletOutputStream ouputStream = response.getOutputStream()

try{

JRExporter exporter = newJRRtfExporter()

exporter.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint)

exporter.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, ouputStream)

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

response.setHeader("Content-Disposition", "attachment;filename="+ downloadFileName + ".doc")

exporter.exportReport()

ouputStream.flush()

} finally{

try{

ouputStream.close()

} catch(Exception e) {

}

}

}

/***导出RTF*/private static voidexportRTF(HttpServletResponse response, JasperPrint jasperPrint, String downloadFileName)

throwsJRException, IOException {

ServletOutputStream ouputStream = response.getOutputStream()

try{

JRExporter exporter = newJRRtfExporter()

exporter.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint)

exporter.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, ouputStream)

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

response.setHeader("Content-Disposition", "attachment;filename="+ downloadFileName + ".rtf")

exporter.exportReport()

ouputStream.flush()

} finally{

try{

ouputStream.close()

} catch(Exception e) {

}

}

}

/***导出html*/private static voidexportHtml(HttpServletResponse response, JasperPrint jasperPrint, String downloadFileName)

throwsJRException, IOException {

ServletOutputStream ouputStream = response.getOutputStream()

try{

JRHtmlExporter exporter = newJRHtmlExporter()

exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint)

exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream)

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

//默认情况下用的是px,会导致字体缩小exporter.setParameter(JRHtmlExporterParameter.SIZE_UNIT,"pt")

//移除空行exporter.setParameter(JRHtmlExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,Boolean.TRUE)

//线条不对齐的解决方法exporter.setParameter(JRHtmlExporterParameter.FRAMES_AS_NESTED_TABLES,Boolean.FALSE)

exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE)

response.setContentType("text/html;charset=utf-8")

exporter.exportReport()

ouputStream.flush()

} finally{

try{

ouputStream.close()

} catch(e) {

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值