ireport報表一

public interface IReportService {
	
	/**
	 * 獲取報表的html 通過List
	 * 
	 * @param list
	 * 			bean的數據庫源
	 * @param response
	 * 			輸出對象
	 * @param url
	 * 			jasper的路徑
	 * @param map
	 * 			報表的參數
	 * @return 報表的字符串
	 * 
	 * @throws JRException
	 * 
	 * 
	 */
	public String getHtmlReportByList(List list , HttpServletResponse response , String url , Map<String , Object> map ) throws JRException;
	
	/**
	 * 獲取報表的html 通過JRDataSource
	 * 
	 * @param ds
	 * 			數據源
	 * @param response
	 * 			輸出對象
	 * @param url
	 * 			jasper的路徑
	 * @param map
	 * 			報表就參數
	 * @return 報表的字符串
	 * 
	 * @throws JRException
	 * 
	 * 
	 */
	public String getHtmlReportByJRDataSource(ReportBaseService ds, HttpServletResponse response , String url , Map<String , Object> parametersMap ) throws JRException;
	
	/**
	 * 根據路徑創建Jasper文件
	 * 
	 * @param path
	 * 			jrxml文件路徑
	 * @return
	 * 		jasper文件路徑
	 * 
	 * @throws JRException
	 * 
	 * 
	 */
	public String getCreateJasper(String path) throws JRException;
	
	
	/**
	 * 獲取項目根目錄路徑
	 * 
	 * @param request
	 * 			請求對象
	 * @param fileUrl
	 * 			除根項目外的文件路徑,不包括文件
	 * @param fileName
	 * 			文件名
	 * @return
	 * 
	 * 
	 */
	public String getFileRealPath(HttpServletRequest request ,String fileUrl,String fileName);
	
	
	
	public void getReportExcelByJRDataSource(ReportBaseService ds, HttpServletResponse response , String url , Map<String , Object> parametersMap ) throws JRException, IOException;
}

@Service("reportService")
public class ReportService implements IReportService{

	@Override
	public String getHtmlReportByList(List list, HttpServletResponse response, String url, Map<String, Object> map) throws JRException {
		
		response.setContentType("text/hmtl;charset=utf-8");
		
		File reportFile = new File(url);
		
		JasperReport jasperReport = null;
		
		//加載jasper文件
		jasperReport = (JasperReport) JRLoader.loadObject(reportFile.getPath());
		
		//數據源
		JRBeanCollectionDataSource jrbean = new JRBeanCollectionDataSource(list);
		
		JasperPrint jasperPrint = null;
		
		//填充數據參數和數據源
		jasperPrint = JasperFillManager.fillReport(jasperReport, map, jrbean);
		
		JRHtmlExporter exporter = new JRHtmlExporter();
		
		StringBuffer sHtml = new StringBuffer();
		
		exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
		
		exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);
		
		exporter.setParameter(JRHtmlExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
		
		exporter.setParameter(JRHtmlExporterParameter.OUTPUT_STRING_BUFFER, sHtml);
		
		exporter.exportReport();
		
		return sHtml.toString();
		
	}

	@Override
	public String getHtmlReportByJRDataSource(ReportBaseService ds,HttpServletResponse response, String url,Map<String, Object> parametersMap) throws JRException {

		response.setContentType("text/hmtl;charset=utf-8");
		
		File reportFile = new File(url);
		
		JasperReport jasperReport = null;
		
		//加載jasper文件
		jasperReport = (JasperReport) JRLoader.loadObject(reportFile.getPath());
		
		//數據源
		JasperPrint jasperPrint = null;
		
		//填充數據參數和數據源
		jasperPrint = JasperFillManager.fillReport(jasperReport, parametersMap, ds);
		
		JRHtmlExporter exporter = new JRHtmlExporter();
		
		StringBuffer sHtml = new StringBuffer();
		
		exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
		
		exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);
		
		exporter.setParameter(JRHtmlExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
		
		exporter.setParameter(JRHtmlExporterParameter.OUTPUT_STRING_BUFFER, sHtml);
		
		exporter.exportReport();
		
		return sHtml.toString();
		
	}

	@Override
	public String getCreateJasper(String path) throws JRException {
		
		JasperCompileManager.compileReportToFile(path);
		
		return getSubFilePath(path);
	}

	private String getSubFilePath(String fileUrl) {
		
		return fileUrl.substring(0, fileUrl.lastIndexOf(".jrxml"))+".jasper";
	}

	@Override
	public String getFileRealPath(HttpServletRequest request, String fileUrl,String fileName) {
		
		return request.getSession().getServletContext().getRealPath("") + File.separator + fileUrl + File.separator + fileName;
	}

	@Override
	public void getReportExcelByJRDataSource(ReportBaseService ds,HttpServletResponse response, String url,Map<String, Object> parametersMap) throws JRException, IOException {

		response.setContentType("application/vnd.excel");
		
		response.setCharacterEncoding("UTF-8");  
		
		response.setHeader("Content-Disposition", "attachment; filename=\"report.xls"); 
		
		File reportFile = new File(url);
		
		JasperReport jasperReport = null;
		
		//加載jasper文件
		jasperReport = (JasperReport) JRLoader.loadObject(reportFile.getPath());
		
		//數據源
		JasperPrint jasperPrint = null;
		
		//填充數據參數和數據源
		jasperPrint = JasperFillManager.fillReport(jasperReport, parametersMap, ds);
		
		JRXlsExporter exporter = new JRXlsExporter();  
		
		OutputStream ouputStream = response.getOutputStream();  
		
		StringBuffer sHtml = new StringBuffer();
		
		exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
		
		exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);
		
		exporter.setParameter(JRHtmlExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
		
		exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
		
		exporter.exportReport();
		
		ouputStream.flush();
		
		ouputStream.close();
		
		response.flushBuffer();
		
		
	}
	
	

}

發佈了39 篇原創文章 · 獲贊 2 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章