1. 程式人生 > >java web jasperreport+ireport 實現多記錄自動分頁列印

java web jasperreport+ireport 實現多記錄自動分頁列印

建立一個printByJasperDemo.jsp,程式碼如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page
import="java.io.*,net.sf.jasperreports.engine.*,net.sf.jasperreports.engine.util.*,java.util.*,java.sql.*,net.sf.jasperreports.engine.export.*"%>
<script type="text/javascript">
function printView() {
window.print();
}
</script>
<!-- 實現列印的關鍵樣式,Noprint:使用該樣式的元素在列印時會被忽略;PageNext:瀏覽器列印介面能夠識別的分頁標記,碰到有該樣式的元素,瀏覽器列印介面就知道要換一頁了 -->
<style media="print" type="text/css">
.Noprint {
display: none;
}
.PageNext {
page-break-after: always;
}
</style>
<button onclick="printView()" class="Noprint">列印預覽</button>
<%
File reportFile = new File(
application.getRealPath("jasper/elecPayPrint.jasper"));
JasperReport jasperReport = (JasperReport) JRLoader
.loadObject(reportFile.getPath());
//Map裡面可以封裝jasper中用到的引數
Map parameters = new HashMap();
parameters.put("make_code", "D_20140116_0053");
parameters.put("make_date", "2012-12-12");
parameters.put("make_man", "雷巨集");
parameters.put("area_name", "長沙");


//資料庫連線
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:@10.46.1.117:1521:property", "hnproperty", "property123");


JasperPrint jasperPrint = JasperFillManager.fillReport(
jasperReport, parameters, conn);


JRHtmlExporter exporter = new JRHtmlExporter();


exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);//待export的japserPrint
exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);//輸出writer
exporter.setParameter(
JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,//去掉圖片填充
Boolean.FALSE);
//列印分頁的關鍵程式碼,結合了上面的樣式
exporter.setParameter(JRHtmlExporterParameter.BETWEEN_PAGES_HTML,
"<div class='PageNext'></div>");
 
exporter.setParameter(
JRHtmlExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,
Boolean.TRUE);

exporter.setParameter(JRHtmlExporterParameter.SIZE_UNIT,
JRHtmlExporterParameter.SIZE_UNIT_POINT);
exporter.setParameter(JRHtmlExporterParameter.SIZE_UNIT,
JRHtmlExporterParameter.SIZE_UNIT_PIXEL); 

exporter.exportReport();


out.flush();
conn.close();
%>