<!-- 本例是一個表格的打印 說明: 1、使用object對象來注冊一個打印控件 2、使用document.getElementById獲取到打印控件,使用CheckLodop檢查是否安裝打印控件,檢驗版本 3、使用c:forEach標簽來遍歷數據,使用EL來讀取屬性 4、使用table來展現數據,使用到了thead、tfoot、tbody,主要是用於分頁打印,這里設置每10行記錄打印一頁 5、使用fmt:formatNumber將數值格式化為2位精度 --> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page isELIgnored="false" %> <%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%> <!DOCTYPE html> <html> <head> <title>打印測試</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <style type="text/css"> </style> <%-- <script type="text/javascript" src="${pageContext.request.contextPath}/lodop/LodopFuncs.js"></script> --%> </head> <!-- 設置一個常量WebRoot,保存當前應用的contextpath --> <script type="text/javascript"> var WebRoot = '${pageContext.request.contextPath}'; </script> <!-- 注冊一個Lodop控件 --> <script type="text/javascript"> var LODOP = document.getElementById("LODOP");//這行語句是為了符合DTD規范 var LODOP=getLodop(document.getElementById('LODOP'),document.getElementById('LODOP_EM')); //CheckLodop(); function create(printodiv) { //LODOP.PRINT_INIT("simple_form"); //假設我們使用A4紙張打印 A4=21cm * 29.7cm //頁邊距我們設置為 1cm (top、left、right、bottom) //width =21-1*2 =21-2=19 //height =29.7-1*2=29.7-2=27.7cm //LODOP.ADD_PRINT_HTM('1cm','1cm','19cm','27.7cm', printodiv.innerHTML); //LODOP.ADD_PRINT_HTM('1cm', '1cm', '19cm', '27.7cm', document.getElementById(printodiv).innerHTML); //A4紙 縱 LODOP.SET_PRINT_PAGESIZE(2, 0, 0, "A4"); LODOP.ADD_PRINT_TEXT('20', '420','600', '50', "換房統計表"); LODOP.SET_PRINT_STYLEA(1, "ItemType", 1); LODOP.SET_PRINT_STYLEA(1, "FontSize", 14); LODOP.SET_PRINT_STYLEA(1, "Bold", 1); LODOP.ADD_PRINT_TEXT('730','660','200','22', "第#頁/共&頁"); LODOP.SET_PRINT_STYLEA(2, "ItemType", 2); LODOP.SET_PRINT_STYLEA(2, "HOrient", 1); LODOP.ADD_PRINT_TABLE('50', '45', '500', '540', document.getElementById(printodiv).innerHTML); } function preview() { create("from_div"); LODOP.PREVIEW(); //預覽 } function print(){ create("from_div"); LODOP.PRINT(); } </script> </script> <body> <form> <div> <a href="javascript:print();">打印</a> <a href="javascript:preview();">預覽</a> <a href="javascript:window.history.back();">返回</a> </div> <div id="from_div" align="center"> <table style="border-color: black; border-collapse: collapse; border-width: 1px; font-size: 20" border="1" cellspacing="0" cellpadding="0" width="1000" align="center"> <!-- <tr> <td colspan="15" align="center"><span style="font-size:14;font-family:宋體;" >換房統計表</span></td> </tr> --> <tr> <td height="20" width="30"><span style="font-size:14;font-family:宋體;">序號</span></td> <td height="20" width="58"><span style="font-size:14;font-family:宋體;"> 換房類型</span></td> <td height="20" width="42" ><span style="font-size:14;font-family:宋體;"> 姓名</span></td> <td height="20" width="45"><span style="font-size:14;font-family:宋體;"> 原房號</span></td> <td height="20" width="65"><span style="font-size:14;font-family:宋體;"> 原房類</span></td> <td height="20" width="30"><span style="font-size:14;font-family:宋體;">床位</span></td> <td height="20" width="42"><span style="font-size:14;font-family:宋體;"> 原房價</span></td> <td height="20" width="48"><span style="font-size:14;font-family:宋體;"> 服務費</span></td> <td height="20" width="45"><span style="font-size:14;font-family:宋體;"> 新房號</span></td> <td height="20" width="65"><span style="font-size:14;font-family:宋體;"> 新房類</span></td> <td height="20" width="30"><span style="font-size:14;font-family:宋體;">床位</span></td> <td height="20" width="42"><span style="font-size:14;font-family:宋體;"> 新房價</span></td> <td height="20" width="48"><span style="font-size:14;font-family:宋體;"> 服務費</span></td> <td height="20" width="115"><span style="font-size:14;font-family:宋體;"> 換房日期</span></td> <td height="20" width="160"><span style="font-size:14;font-family:宋體;"> 換房原因</span></td> </tr> <c:forEach items="${querylist}" var="s" varStatus="status"> <tr> <td height="20" width="30" align="center"><span style="font-size:12;font-family:宋體;">${status.index+1}</td> <c:choose> <c:when test="${s.changeroomType} == 1"> <td height="20" width="58"><span style="font-size:12;font-family:宋體;">換空房</span></td> </c:when> <c:otherwise><td height="20" width="58"><span style="font-size:12;font-family:宋體;">房間對換</span></td></c:otherwise> </c:choose> <td height="20" width="42" ><span style="font-size:12;font-family:宋體;">${s.nameBefore}</span></td> <td height="20" width="45" align="center"><span style="font-size:12;font-family:宋體;">${s.zoneAndNum}</span></td> <td height="20" width="65"><span style="font-size:12;font-family:宋體;">${s.roomtypeBefore}</span></td> <td height="20" width="30" align="center"><span style="font-size:12;font-family:宋體;">${s.bednumBefore}</span></td> <td height="20" width="42" align="right"><span style="font-size:12;font-family:宋體;">${s.rateBefore}</span></td> <td height="20" width="48" align="right"><span style="font-size:12;font-family:宋體;">${s.servicechargeBefore}</span></td> <td height="20" width="45" align="center"><span style="font-size:12;font-family:宋體;">${s.zoneAndNumNew}</span></td> <td height="20" width="65"><span style="font-size:12;font-family:宋體;">${s.roomtypeNew}</span></td> <td height="20" width="30" align="center"><span style="font-size:12;font-family:宋體;">${s.bednumNew}</span></td> <td height="20" width="42" align="right"><span style="font-size:12;font-family:宋體;">${s.rateNew}</span></td> <td height="20" width="48" align="right"><span style="font-size:12;font-family:宋體;">${s.servicechargeNew}</span></td> <td height="20" width="115" align="center"><span style="font-size:12;font-family:宋體;">${s.changeroomDate}</span></td> <td height="20" width="160"><span style="font-size:12;font-family:宋體;">${s.changeroomCause}</span></td> </tr> </c:forEach> </table> </div> </form> </body> </html>