package com.appdev.bsf.server.servlet;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRAbstractExporter;
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.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRHtmlExporter;
import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.export.JRRtfExporter;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
import net.sf.jasperreports.engine.util.JRLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.appdev.bsf.common.client.datasource.DictionaryGwtRpcService;
@SuppressWarnings("serial")
public class ReportServlet extends HttpServlet {
??? private DictionaryGwtRpcService service;
??? @Override
??? public void init() throws ServletException {
??? ??? ServletContext application;
??? ??? WebApplicationContext wac;
??? ??? application = getServletContext();
??? ??? wac = WebApplicationContextUtils.getWebApplicationContext(application);
??? ??? service = (DictionaryGwtRpcService) wac.getBean("dictionaryGwtRpcServiceImpl");
??? }
??? @SuppressWarnings("deprecation")
??? @Override
??? protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException,
??? ??? ??? IOException {
??? ??? JasperReport jReport = null;
??? ??? try {
??? ??? ??? String jasperName = "";
??? ??? ??? Map<String, Object> filter = new HashMap<String, Object>();
??? ??? ??? String clsUrl = request.getParameter("clsUrl");
??? ??? ??? String type = request.getParameter("type");
??? ??? ??? String map = request.getParameter("map");
??? ??? ??? if (clsUrl != null && !"".equals(clsUrl)) {
??? ??? ??? ??? String[] jasperNames = clsUrl.split("\\.");
??? ??? ??? ??? jasperName = jasperNames[jasperNames.length - 1];
??? ??? ??? }
??? ??? ??? if (map != null) {
??? ??? ??? ??? map = map.replace("{", "").replace("}", "");
??? ??? ??? ??? String maps[] = map.split(",");
??? ??? ??? ??? for (String str : maps) {
??? ??? ??? ??? ??? String[] obj = str.split("=");
??? ??? ??? ??? ??? if (obj.length == 2) {
??? ??? ??? ??? ??? ??? filter.put(obj[0], obj[1]);
??? ??? ??? ??? ??? }
??? ??? ??? ??? }
??? ??? ??? }
??? ??? ??? if (type == null) {
??? ??? ??? ??? type = "Excel";
??? ??? ??? }
??? ??? ??? List<?> list = service.fetch(clsUrl, filter);
??? ??? ??? if (list.size() > 0) {
??? ??? ??? ??? Map<String, String> paramMap = new HashMap<String, String>();
??? ??? ??? ??? paramMap.put("ReportTitle", jasperName);
??? ??? ??? ??? String filePath = request.getRealPath("/report/") + jasperName + ".jasper";
??? ??? ??? ??? File file = new File(filePath);
??? ??? ??? ??? InputStream is = new FileInputStream(file);
??? ??? ??? ??? JasperPrint jasperPrint = null;
??? ??? ??? ??? jReport = (JasperReport) JRLoader.loadObject(is);
??? ??? ??? ??? JRBeanCollectionDataSource jrDataSource = new JRBeanCollectionDataSource(list);
??? ??? ??? ??? jasperPrint = JasperFillManager.fillReport(jReport, paramMap, jrDataSource);
??? ??? ??? ??? OutputProcessed ouputProcessed = OutputProcessed.processOutput(type);
??? ??? ??? ??? JRAbstractExporter exporter = ouputProcessed.exporter;
??? ??? ??? ??? byte[] reportAsBytes;
??? ??? ??? ??? ByteArrayOutputStream baos = new ByteArrayOutputStream();
??? ??? ??? ??? exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
??? ??? ??? ??? exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
??? ??? ??? ??? exporter.exportReport();
??? ??? ??? ??? reportAsBytes = baos.toByteArray();
??? ??? ??? ??? response.reset();
??? ??? ??? ??? // Writes the bytes[] in the response
??? ??? ??? ??? response.setContentType(ouputProcessed.contentType);
??? ??? ??? ??? response.setHeader("Content-Disposition", "inline; filename=" + jasperName
??? ??? ??? ??? ??? ??? + ouputProcessed.fileType);
??? ??? ??? ??? response.setContentLength(reportAsBytes.length);
??? ??? ??? ??? ServletOutputStream s = response.getOutputStream();
??? ??? ??? ??? s.write(reportAsBytes, 0, reportAsBytes.length);
??? ??? ??? ??? s.flush();
??? ??? ??? ??? s.close();
??? ??? ??? }
??? ??? } catch (Exception ex) {
??? ??? ??? ex.printStackTrace();
??? ??? }
??? }
}
class OutputProcessed {
??? public JRAbstractExporter exporter;
??? public String contentType;
??? public String fileType;
??? public static final String PDF = "PDF";
??? public static final String HTML = "HTML";
??? public static final String EXCEL = "Excel";
??? public static final String RTF = "RTF";
??? public static OutputProcessed processOutput(String output) {
??? ??? OutputProcessed result = new OutputProcessed();
??? ??? if (output.equals(PDF)) {
??? ??? ??? result.contentType = "application/pdf";
??? ??? ??? result.exporter = new JRPdfExporter();
??? ??? ??? result.fileType = ".pdf";
??? ??? } else if (output.equals(HTML)) {
??? ??? ??? result.contentType = "text/html";
??? ??? ??? result.exporter = new JRHtmlExporter();
??? ??? ??? result.exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);
??? ??? ??? result.fileType = ".html";
??? ??? } else if (output.equals(EXCEL)) {
??? ??? ??? result.contentType = "application/vnd.ms-excel";
??? ??? ??? result.exporter = new JRXlsExporter();
??? ??? ??? result.exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
??? ??? ??? result.exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,
??? ??? ??? ??? ??? Boolean.TRUE);
??? ??? ??? result.fileType = ".xls";
??? ??? } else {
??? ??? ??? result.contentType = "application/rtf";
??? ??? ??? result.exporter = new JRRtfExporter();
??? ??? ??? result.fileType = ".rtf";
??? ??? }
??? ??? return result;
??? }
}
详细解决方案
iReport,jasperReport表格打印
热度:1159 发布时间:2012-07-02 17:46:22.0
相关解决方案
- struts2+iReport 子表格 急需高手解决,已经困惑三天了!
- jaspersoft ireport,该如何处理
- jasperreport 输出html ,字段里有<> 等都被替换成<>该如何解决
- 报表 jasperreport+ireport解决思路
- ireport 生成报表有关问题
- 8.30加班中. ireport 图形报表
- jasperreport 导出pdf 有关问题
- 在程序里如何给交叉报表(JasperReport)传递参数
- jasperreport flash viewer 棘手有关问题
- jasperreport 经过javabean datasoource实现chart的报表
- [转]一次 applet 客户端打印 jasperreport 表格任务笔记
- Jasperreport+applet兑现客户端打印
- iReport+jasperreports生成各种格式的表格
- JasperReport subdataset datasetRun的有关设置
- 应用 jasperreport 来打印报表
- IReport | Jasperreport 无数据或查询结果集行数替0 依然展现报表
- jasperReport+jsp 奇怪错误 tried to write 1544 bytes with content-length 1439
- JasperReport+iReport+Applet+Servlet打造报表
- jasperreport 自学札记
- iReport-4.0.2(jasperreports-4.0.2) 解决中文有关问题
- JasperReport 在Spring中的使用 有关问题
- jasperreport 内容替空的处理
- 改建struts2-jasperreport-plugin-2.1.6支持多数据源
- iReport and jasperReports 图文课程
- JasperReport 依据内容动态调整报表行高
- JasperReport 组合iReport报表开发
- (转)JasperReport 种介绍
- jasperreport 组合struts2开发报表
- iReport,jasperReport表格打印
- JasperReport(二)――IReport使用数据源