jasper报表核心代码

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
没有配置文件,配置都由同事做完了。中文输出好像有点问题,但大体是实现了的

/**
	 * 
	 * @函数名称:previewCityInfo  controller层
	 * @创建日期:2019年4月19日
	 * @功能说明:
	 * @参数说明:
	 * @返回说明:void
	 */
	@RequestMapping(value = "/preview_cityInfo")
	public void previewCityInfo(HttpServletRequest request, HttpServletResponse response) {
		// 拿到session
		Session session = SecurityUtils.getSubject().getSession();
		CityInfoQ cityCodeQ = (CityInfoQ) session.getAttribute("cityCodeQ");
		List<Order> orders = this.generateOrders(cityCodeQ);
		List<Where> wheres = this.generateWheres(cityCodeQ);
		Map<String,String> countryMap = new HashMap<String,String>();
		Pager<CityInfo> pager = new Pager<>(0, 20);
		try {
			// 分页查询所有城市信息
			pager = cityInfoService.getCityInfos(cityCodeQ.getNumPerPage(), cityCodeQ.getPageNum(), null, wheres, orders);
			Map<String, Object> maps = new HashMap<String, Object>();
			maps.put("cityCodeQ", cityCodeQ.getCityCodeQ());
			maps.put("cityNameQ", cityCodeQ.getCityNameQ());
			maps.put("countryCodeQ", cityCodeQ.getCountryCodeQ());
			cityInfoService.journalPreview(response, pager.getResults(), maps);
		} catch (Exception e) {
			log.error("cityInfo log pager query:", e);
		}
		
	}
	
	/**serviceImpl层
 * 报表
 */
	@Override
	public void journalPreview(HttpServletResponse response, List<CityInfo> results, Map<String, Object> maps) throws Exception {
		// TODO Auto-generated method stub
		File reportFile = null;

			reportFile = ResourceUtils.getFile("classpath:reports/firstReport.jasper");

		JasperReport jasperReport = (JasperReport) JRLoader.loadObject(reportFile);
		JasperPrint jasperPrint = null;
		try {
			jasperPrint = JasperFillManager.fillReport(jasperReport, maps, new JRBeanCollectionDataSource(results));
		} catch (JRException e) {
			e.printStackTrace();
		}
		if (null != jasperPrint) {
			FileBufferedOutputStream fbos = new FileBufferedOutputStream();
			JRPdfExporter exporter = new JRPdfExporter();
			exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, fbos);
			exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
			try {
				exporter.exportReport();
				fbos.close();
				if (fbos.size() > 0) {
					response.setContentType("application/pdf");
					response.setContentLength(fbos.size());
					ServletOutputStream ouputStream = response.getOutputStream();
					try {
						fbos.writeData(ouputStream);
						fbos.dispose();
						ouputStream.flush();
					} finally {
						if (null != ouputStream) {
							ouputStream.close();
						}
					}
				}
			} catch (JRException e1) {
				e1.printStackTrace();
			} finally {
				if (null != fbos) {
					fbos.close();
					fbos.dispose();
				}
			}
		}
	}
	
	
	/*html 页面*/
	<td><a class="edit" id = "printId" "printJournalPreview()" title="打印日报"><span>打印</span></a></td>
	<div class="loading" style="display: none;font-weight:800;color:#fff;font-size: 16px;position: absolute;top: 50%;left: 40%;z-index: 9999999999;transform: translate(-50%,-50%);">加载中...</div>
	<div id="bground_print" style="display: none;position: fixed; top: 0px; left: 0px; width: 100%; height: 100%; z-index: 10000; background: rgb(119, 119, 119); opacity: 0.6;"></div>
	<div id="content_print" style="display: none;z-index:999999999;position: absolute;top: 57%;left: 40%;transform: translate(-50%,-50%);"></div>
	
	
	<script type="text/javascript">
	function printJournalPreview(){
	var tr="";
	tr=document.getElementsByName('printreport');
	
	if (tr.length <= 0) {
		alertMsg.error("打印数据不能为空!");
	}else{
			$(".loading").show();
  		    //  EV_modeAlert();//弹出遮罩层
  			var iframe = '<div style="position:fixed;top: 52px;right: 20px;color:#fff;z-index:1;"> <button style="with:80px;height:33px;font-size: 20px;" type="button" onclick ="closeIframe()">关闭</button></div><iframe target="target1" src="/city_info/preview_cityInfo?flag=true" target="_blank" width="900" height="500" scrolling="no" border="0" ></iframe>';
  			//延时加载
  			setTimeout(function(){ 
  		 	$(".loading").hide();
  			$("#content_print").append(iframe);
		}, 1000);
  			
   		$("#bground_print").show();//显示遮盖层 
   		$("#content_print").show();//显示隐藏的区域
	    
	}
}

//关闭打印的弹出区域
function closeIframe(){
   $("#bground_print").hide();
   $("#content_print").hide();
   $("#content_print").empty();
}
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值