如果要求每页都有表头,定义其thead,并对其样式设置成:style="display:table-header-group"。如果要求有表尾,也一样,要定义其tfoot,并对style="display:table-footer-group"
<div id="report"> <table> <thead> <tr> <th>表头</th> </tr> </thead> <tbody> <tr> <td>数据</td> </tr> </tbody> </table> </div>
goPrint() { console.log(‘打印????‘); // window.print(); // 如果直接 print, 可以用 @media print { selector { ... } } 插入样式
if (!(<any>window).ActiveXObject || "ActiveXObject" in window) { //是否ie console.log(‘是否ie‘) this.remove_ie_header_and_footer(); } let printHtml = document.getElementById(‘report‘).outerHTML; var iframe = document.createElement("iframe"); document.getElementsByTagName("body")[0].appendChild(iframe); var doc = iframe.contentDocument || iframe.contentWindow.document; // doc.write(<style> table{} </style>) // 可以直接插入样式 doc.write(printHtml); iframe.contentWindow.focus(); iframe.contentWindow.print(); doc.close(); document.body.removeChild(iframe); } remove_ie_header_and_footer() { var hkey_path; hkey_path = "HKEY_CURRENT_USER\\Software\\Microsoft\\Internet Explorer\\PageSetup\\"; try { var RegWsh = new ActiveXObject("WScript.Shell"); RegWsh.RegWrite(hkey_path + "header", ""); RegWsh.RegWrite(hkey_path + "footer", ""); } catch (e) {} }