日期:2014-05-17  浏览次数:21025 次

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.loa