当前位置: 代码迷 >> ASP >> iReport,jasperReport表格打印
  详细解决方案

iReport,jasperReport表格打印

热度:1159   发布时间:2012-07-02 17:46:22.0
iReport,jasperReport报表打印

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;
??? }
}