JasperReport出力Excelレポート

2748 ワード

Excel形式:
/**  
*    
*   
* @param request  
* @param response  
* @param reportFilePath  
* @param params  
* @param con  
* @param fileName  
* @throws JasperReportException  
*/   
public void export(HttpServletRequest request,
		   HttpServletResponse response, 
		   String reportFilePath, Map params,   
		   Connection con,
		   String fileName) throws JasperReportException {   
	JasperPrint jasperPrint = new JasperPrintWithConnection(reportFilePath, params, con).getJasperPrint();   
	//  japserPrint session 。   
	request.getSession().setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);   
	//  japserPrintList   
	List jasperPrintList = BaseHttpServlet.getJasperPrintList(request);   
	//  JasperPrintList,    
	if (jasperPrintList == null) {   
	    throw new JasperReportException(" Http Session JasperPrint List");   
	}   
	try {   
	    OutputStream ouputStream = response.getOutputStream();   
	    try { 
		response.setContentType("application/xls");   
		response.setCharacterEncoding("UTF-8");   
		if (fileName == null || fileName.equals(""))   
		    response.setHeader("Content-Disposition", "inline; filename=\"noTitle.xls\"");   
		else {   
		    response.setHeader("Content-Disposition", "inline; filename=\""   
			    + URLEncoder.encode(fileName, "UTF-8") + ".xls\"");  

		}   
		// Xls  JRXlsAbstractExport   
		JRXlsAbstractExporter exporter = getXlsExporter();   

		//  japserPrintList   
		exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);   

		exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);   
		//  Xls    
		exporter.setParameter(JRXlsAbstractExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);   
		exporter.setParameter(JRXlsAbstractExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);   
		//     
		exporter.exportReport();   
	    } catch (JRException e) {   
		e.printStackTrace();   
		throw new JasperReportException(" XLS !");   
	    } finally {   
		if (ouputStream != null) {   
		    try {   
			ouputStream.close();   
		    } catch (IOException ex) {   
		    }   
		}   
	    }   
	} catch (IOException ioe) {   
	    ioe.printStackTrace();   
	    throw new JasperReportException(" Response OutputStream !");   
	} 
}