如何使用VB.NET禁用SAP Crystal Reports打印预览 直接打印到默认打印机
无预览直接打印小票实现方案
你可以直接调用Crystal Reports中ReportDocument对象的PrintToPrinter方法,跳过预览窗口直接将打印任务发送到系统默认打印机,替换原有触发预览的代码即可。
修改后代码
Private Sub BTPRINT_Click(sender As Object, e As EventArgs) Handles BTPRINT.Click Dim dt As New DataTable With dt .Columns.Add("Articulo") .Columns.Add("Cantidad") .Columns.Add("Precio") End With For Each dgr As DataGridViewRow In Me.DataGridView1.Rows dt.Rows.Add(dgr.Cells(0).Value, dgr.Cells(1).Value, dgr.Cells(2).Value) Next Dim reportdocument As CrystalDecisions.CrystalReports.Engine.ReportDocument reportdocument = New CrystalReport2 reportdocument.SetDataSource(dt) ' 跳过预览直接打印到默认打印机,参数可按需调整 reportdocument.PrintToPrinter(1, False, 0, 0) ' 释放报表资源 reportdocument.Close() reportdocument.Dispose() End Sub
参数说明
PrintToPrinter方法4个传入参数含义如下:
- 第1位:打印份数,为整数类型,示例中1代表打印1份
- 第2位:是否逐份打印,为布尔类型,单份打印时可设为False
- 第3位:打印起始页码,设为0代表无起始页限制,从第一页开始打印
- 第4位:打印结束页码,设为0代表打印报表全部页面
注意事项
- 提前在CrystalReport2的设计界面配置好对应小票的纸张尺寸、边距参数,避免打印内容错位
- 确保运行程序的设备默认打印机配置正常,程序拥有打印机访问权限
- 如需指定非默认打印机打印,可先通过
reportdocument.PrintOptions.PrinterName设置目标打印机名称,再调用打印方法
内容的提问来源于stack exchange,提问作者jumexmango
相关产品推荐
相关产品推荐

