【发布时间】:2017-12-08 08:46:32
【问题描述】:
我试图在一页内打印数据表。其中有 200 或 300 行。它应该像在线 MS Excel 打印选项 -> 将表格放在一页上一样打印在单页中。我怎样才能做到这一点?
我试过下面的代码,
var tble = document.createElement("table");
tble.id = "tble";
document.body.appendChild(tble);
var trtd = "<tr><td>123</td></tr>"
for (var i = 0; i < 300; i++) {
$("#tble").append(trtd);
}
var divs = "<div id='printing'></div>";
$("#printing").append($("#tble"));
$("#printData").click(function() {
var printToDiv = $("#printing");
var newWin = window.open("", "print-window");
newWin.document.open();
newWin.document.write('<html><body onload="window.print()">' + printToDiv.innerHTML + '</body></html>');
newWin.document.close();
setTimeout(function() {
newWin.close();
}, 10);
})
印刷的内容应该在一页上。有什么办法吗?
【问题讨论】:
-
我会搜索类似css fit din a4 的内容,Google 会在那里找到一些有趣的 Stackoverflow 帖子。
-
也许您可以从您的数据中生成 PDF 或 Excel 文件并让用户自己打印出来?
标签: javascript jquery css excel printing