VB.NET中DataTable转HTML打印:含页眉/纸张设置等需求求助
VB.NET DataTable转HTML打印完整实现
1. 优化HTML生成代码(含打印样式)
直接生成带完整打印规则的HTML,包含标题页眉、发票号、总计,同时指定A5横向、页面自适应:
Private Function DataTableToPrintHtml(dt As DataTable, title As String, invoiceNo As String) As String ' 计算金额总计 Dim totalAmount As Decimal = 0 For Each row As DataRow In dt.Rows If Not IsDBNull(row("金额")) Then totalAmount += Convert.ToDecimal(row("金额")) End If Next ' 构建HTML字符串,内嵌打印专用CSS Dim htmlBuilder As New StringBuilder() htmlBuilder.AppendLine("<!DOCTYPE html>") htmlBuilder.AppendLine("<html>") htmlBuilder.AppendLine("<head>") htmlBuilder.AppendLine("<meta charset='utf-8'>") htmlBuilder.AppendLine("<title>" & title & "</title>") htmlBuilder.AppendLine("<style>") ' 通用显示样式 htmlBuilder.AppendLine("body { font-family: 微软雅黑, Arial; margin: 0; padding: 10px; }") htmlBuilder.AppendLine(".header { text-align: center; margin-bottom: 20px; }") htmlBuilder.AppendLine(".header h1 { margin: 0; font-size: 18px; }") htmlBuilder.AppendLine(".invoice-no { text-align: right; margin-bottom: 15px; font-size: 14px; }") htmlBuilder.AppendLine("table { width: 100%; border-collapse: collapse; margin-top: 10px; }") htmlBuilder.AppendLine("th, td { border: 1px solid #000; padding: 8px; text-align: center; font-size: 12px; }") htmlBuilder.AppendLine("th { background-color: #f0f0f0; font-weight: bold; }") htmlBuilder.AppendLine(".total-row { font-weight: bold; font-size: 14px; background-color: #f5f5f5; }") ' 打印专用样式(媒体查询) htmlBuilder.AppendLine("@media print {") htmlBuilder.AppendLine(" @page {") htmlBuilder.AppendLine(" size: A5 landscape; /* A5横向设置 */") htmlBuilder.AppendLine(" margin: 1cm; /* 页边距 */") htmlBuilder.AppendLine(" }") htmlBuilder.AppendLine(" body { padding: 0; }") htmlBuilder.AppendLine(" .header, .invoice-no { page-break-inside: avoid; }") htmlBuilder.AppendLine(" table { page-break-inside: auto; }") htmlBuilder.AppendLine(" tr { page-break-inside: avoid; page-break-after: auto; }") htmlBuilder.AppendLine("}") htmlBuilder.AppendLine("</style>") htmlBuilder.AppendLine("</head>") htmlBuilder.AppendLine("<body>") ' 标题页眉 htmlBuilder.AppendLine("<div class='header'>") htmlBuilder.AppendLine("<h1>" & title & "</h1>") htmlBuilder.AppendLine("</div>") ' 发票号页眉 htmlBuilder.AppendLine("<div class='invoice-no'>") htmlBuilder.AppendLine("发票号: " & invoiceNo) htmlBuilder.AppendLine("</div>") ' 数据表格 htmlBuilder.AppendLine("<table>") ' 表头 htmlBuilder.AppendLine("<thead>") htmlBuilder.AppendLine("<tr>") For Each col As DataColumn In dt.Columns htmlBuilder.AppendLine("<th>" & col.ColumnName & "</th>") Next htmlBuilder.AppendLine("</tr>") htmlBuilder.AppendLine("</thead>") ' 表体数据行 htmlBuilder.AppendLine("<tbody>") For Each row As DataRow In dt.Rows htmlBuilder.AppendLine("<tr>") For Each col As DataColumn In dt.Columns htmlBuilder.AppendLine("<td>" & If(IsDBNull(row(col)), "", row(col).ToString()) & "</td>") Next htmlBuilder.AppendLine("</tr>") Next ' 总计行 htmlBuilder.AppendLine("<tr class='total-row'>") htmlBuilder.AppendLine("<td colspan='" & (dt.Columns.Count - 1) & "' style='text-align: right;'>总计:</td>") htmlBuilder.AppendLine("<td>" & totalAmount.ToString("C2") & "</td>") htmlBuilder.AppendLine("</tr>") htmlBuilder.AppendLine("</tbody>") htmlBuilder.AppendLine("</table>") htmlBuilder.AppendLine("</body>") htmlBuilder.AppendLine("</html>") Return htmlBuilder.ToString() End Function
2. 打印预览与直接打印实现
使用WebBrowser控件加载生成的HTML,利用其内置的打印功能完成需求:
步骤1:添加WebBrowser控件
在窗体设计器中拖入一个WebBrowser控件,可设置Visible = False隐藏显示。
步骤2:打印预览代码
Private Sub btnPrintPreview_Click(sender As Object, e As EventArgs) Handles btnPrintPreview.Click ' 替换为你的DataTable、标题、发票号 Dim htmlContent As String = DataTableToPrintHtml(yourDataTable, "销售明细单", "INV-20240520-001") WebBrowser1.DocumentText = htmlContent ' 等待文档加载完成后触发预览 AddHandler WebBrowser1.DocumentCompleted, AddressOf WebBrowser_DocumentCompleted_Preview End Sub Private Sub WebBrowser_DocumentCompleted_Preview(sender As Object, e As WebBrowserDocumentCompletedEventArgs) WebBrowser1.ShowPrintPreviewDialog() ' 移除事件避免重复触发 RemoveHandler WebBrowser1.DocumentCompleted, AddressOf WebBrowser_DocumentCompleted_Preview End Sub
步骤3:直接打印代码
Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click Dim htmlContent As String = DataTableToPrintHtml(yourDataTable, "销售明细单", "INV-20240520-001") WebBrowser1.DocumentText = htmlContent AddHandler WebBrowser1.DocumentCompleted, AddressOf WebBrowser_DocumentCompleted_Print End Sub Private Sub WebBrowser_DocumentCompleted_Print(sender As Object, e As WebBrowserDocumentCompletedEventArgs) ' 直接打印(不弹出对话框) WebBrowser1.Print() RemoveHandler WebBrowser1.DocumentCompleted, AddressOf WebBrowser_DocumentCompleted_Print End Sub
关键说明
- 打印规则控制:通过
@media print和@page强制指定A5横向纸张、页边距,确保打印参数生效。 - 页面自适应:表格设为
width:100%,内容会自动适配A5横向页面宽度。 - 分页优化:用
page-break-inside: avoid避免页眉、数据行被分页截断。 - 总计计算:遍历DataTable统计金额,最后在表格底部添加加粗总计行。
内容的提问来源于stack exchange,提问作者user22579796
相关产品推荐
相关产品推荐

