利用 ASP 產生動態 HTML 表格,配合瀏覽器原生列印功能,無需 ActiveX/外掛即可實現報表列印。本文以 SQL Server pubs 資料庫 sales 表為例,示範完整列印導向頁面製作。
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>訂單報表</title></head>
<body><form>
<table border="0" width="800" cellspacing="0" cellpadding="0">
<tr><td><div align="center"><center><h2><strong>訂單報表</strong></h2></center></div></td></tr>
<tr><td><div align="right">單位:元</div></td></tr></table>
<table border="1" width="800" cellspacing="0" cellpadding="0"><tr>
<td width="25%" align="center">stor_id</td>
<td width="25%" align="center">ord_num</td>
<td width="25%" align="center">ord_date</td>
<td width="25%" align="center">qty</td></tr>
<%
Set cn = Server.CreateObject("ADODB.Connection")
cn.Open "Provider=SQLOLEDB;Data Source=202.203.208.1;Initial Catalog=pubs;User ID=sa;Password=;"
Sql = "SELECT stor_id, ord_num, ord_date, qty FROM sales WHERE ord_date > '1994-5-1'"
Set rs = cn.Execute(Sql)
Dim sumqtyv : sumqtyv = 0
Do While Not rs.EOF
Dim qtyv : qtyv = rs("qty")
sumqtyv = sumqtyv + qtyv
%>
<tr>
<td><%=rs("stor_id")%></td>
<td><%=rs("ord_num")%></td>
<td><%=rs("ord_date")%></td>
<td><%=qtyv%></td>
</tr>
<%
rs.MoveNext
Loop
%>
<tr>
<td colspan="3">合計</td>
<td><%=sumqtyv%></td>
</tr>
</table>
<% cn.Close %>
</form></body></html>
width="800" 適配 A4 橫向列印table width 值 控制報表整體縮放@media print CSS 隱藏非列印元素(按鈕、導航欄)<style>
@media print {
.no-print { display: none; }
.page-break { page-break-after: always; }
}
</style>
在需分頁處插入 <div class="page-break"></div>。
「ASP 生成 HTML + 瀏覽器列印」是零成本、高相容性的輕量級報表方案。