如何使用 VB.NET 打印发票?






4.89/5 (22投票s)
这是一个使用 VB.NET 打印发票的尝试。
引言
如何使用 VB.NET 打印发票?这是一个使用 VB.NET 打印发票的尝试。
您还可以阅读另一篇文章(如何使用 VB6 打印发票?)。
我的项目包含三个窗体
frmInvoice
:将DataGrid
与 Northwind 数据库文件中的所有Order
绑定frmInput
:选择您想要打印其Invoice
的一个Order
frmOrder
:在DataGrid
上显示Invoice
,然后您可以预览或打印Invoice
作为Report
我们还需要三个类用于打印
System.Windows.Forms.PrintDialog
System.Windows.Forms.PrintPreviewDialog
System.Drawing.Printing.PrintDocument
当然,您可以使用任何数据库文件代替 Northwind.mdb,并更改我的代码以连接到您的数据库文件。您还可以更改我的 SQL string
以绑定 DataGrid
中的数据。
Using the Code
将 frmInvoice
窗体中的 DataGrid
与所有 Orders
绑定
' following lines to connect with access database file 'Northwind.mdb'
Dim MyPass As String = ""
Dim MyDataFile As String = Application.StartupPath & "\DataFile\Northwind.mdb"
Dim strCon As String = "provider=microsoft.jet.oledb.4.0;data source=" _
& MyDataFile & ";" & "Jet OLEDB:Database Password=" & MyPass & ";"
' If you are using SQL Server, please replace previous lines with following:
Dim strCon As String = "provider=sqloledb;Data Source=PC;Initial Catalog=" _
& "Northwind;Integrated Security=SSPI" & ";"
' and replace 'Data Source=PC' with the name of your system
Try
' Get data from tables: Orders, Customers, Employees, Products, Order Details:
Dim InvSql As String = "SELECT Customers.CompanyName, Customers.City, " _
& "Employees.FirstName + Space(1) + Employees.LastName AS Salesperson, " _
& "Orders.OrderID, Orders.OrderDate, " _
& "[Order Details].ProductID, Products.ProductName, [Order Details].UnitPrice, " _
& "[Order Details].Quantity, [Order Details].Discount, " _
& "CCur([Order Details].UnitPrice*[Quantity]*_
(1-[Discount])/100)*100 AS ExtendedPrice, " _
& "Orders.Freight " _
& "FROM Products INNER JOIN ((Employees INNER JOIN " _
& "(Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID) " _
& "ON Employees.EmployeeID = Orders.EmployeeID) " _
& "INNER JOIN [Order Details] ON Orders.OrderID = [Order Details].OrderID) " _
& "ON Products.ProductID = [Order Details].ProductID;"
' create an OleDbDataAdapter
Dim datAdp As OleDbDataAdapter = New OleDbDataAdapter(InvSql, strCon)
' create a command builder
Dim cBuilder As OleDbCommandBuilder = New OleDbCommandBuilder(datAdp)
' create a DataTable to hold the query results
Dim dTable As DataTable = New DataTable
' fill the DataTable
datAdp.Fill(dTable)
' set DataSource of DataGrid
datGrid.DataSource = dTable
Catch ex As Exception
MessageBox.Show(ex.ToString())
End Try
将 frmOrder
窗体中的 DataGrid
与一个 Order
绑定
'InvoiceOrder is the number of Order which you select:
Dim intOrder As Integer = Int32.Parse(InvoiceOrder)
Dim MyDataFile As String = Application.StartupPath & "\DataFile\Northwind.mdb"
Dim MyPass As String = ""
Dim strCon As String = "provider=microsoft.jet.oledb.4.0;data source=" _
& MyDataFile & ";" & "Jet OLEDB:Database Password=" & MyPass & ";"
Try
' Get Invoice Data:
InvSql = "SELECT [Order Details].ProductID, " _
& "Products.ProductName, [Order Details].UnitPrice, " _
& "[Order Details].Quantity, [Order Details].Discount, " _
& "CCur([Order Details].UnitPrice*[Quantity]*(1-[Discount])/100)*100 " _
& "AS ExtendedPrice " _
& "FROM Products INNER JOIN [Order Details] " _
& "ON Products.ProductID=[Order Details].ProductID " _
& "WHERE [Order Details].OrderID = " & intOrder
' create an OleDbDataAdapter
Dim datAdp As OleDbDataAdapter = New OleDbDataAdapter(InvSql, strCon)
' create a command builder
Dim cBuilder As OleDbCommandBuilder = New OleDbCommandBuilder(datAdp)
' create a DataTable to hold the query results
Dim dTable As DataTable = New DataTable
' fill the DataTable
datAdp.Fill(dTable)
' Create a TableStyle to format Datagrid columns.
ordGrid.TableStyles.Clear()
Dim tableStyle As DataGridTableStyle = New DataGridTableStyle
For Each dc As DataColumn In dTable.Columns
Dim txtColumn As DataGridTextBoxColumn = New DataGridTextBoxColumn
txtColumn.MappingName = dc.ColumnName
txtColumn.HeaderText = dc.Caption
Select Case (dc.ColumnName.ToString())
Case "ProductID" ' Product ID
txtColumn.HeaderText = "Product ID"
txtColumn.Width = 60
Case "ProductName" ' Product Name
txtColumn.HeaderText = "Product Name"
txtColumn.Width = 110
Case "UnitPrice" ' Unit Price
txtColumn.HeaderText = "Unit Price"
txtColumn.Format = "0.00"
txtColumn.Alignment = HorizontalAlignment.Right
txtColumn.Width = 60
Case "Discount" ' Discount
txtColumn.HeaderText = "Discount"
txtColumn.Format = "p" 'percent
txtColumn.Alignment = HorizontalAlignment.Right
txtColumn.Width = 60
Case "Quantity" ' Quantity
txtColumn.HeaderText = "Quantity"
txtColumn.Alignment = HorizontalAlignment.Right
txtColumn.Width = 50
Case "ExtendedPrice" ' Extended Price
txtColumn.HeaderText = "Extended Price"
txtColumn.Format = "0.00"
txtColumn.Alignment = HorizontalAlignment.Right
txtColumn.Width = 90
End Select
tableStyle.GridColumnStyles.Add(txtColumn)
Next
tableStyle.MappingName = dTable.TableName
ordGrid.TableStyles.Add(tableStyle)
' set DataSource of DataGrid
ordGrid.DataSource = dTable.DefaultView
Catch ex As Exception
MessageBox.Show(ex.ToString())
End Try
声明并初始化三个用于打印的实例
Private prnDialog As System.Windows.Forms.PrintDialog
Private prnPreview As System.Windows.Forms.PrintPreviewDialog
Private prnDocument As System.Drawing.Printing.PrintDocument
Me.prnDialog = New System.Windows.Forms.PrintDialog
Me.prnPreview = New System.Windows.Forms.PrintPreviewDialog
Me.prnDocument = New System.Drawing.Printing.PrintDocument
' the Event of 'PrintPage'
AddHandler prnDocument.PrintPage, AddressOf prnDocument_PrintPage
要在报告上绘制一些内容(如线条或文本)
- 获取左边距、右边距、上边距、下边距、报告宽度和报告高度
' Result of the Event 'PrintPage' Private Sub prnDocument_PrintPage(ByVal sender As System.Object, _ ByVal e As System.Drawing.Printing.PrintPageEventArgs) leftMargin = Convert.ToInt32_ (e.MarginBounds.Left) ' leftMargin, rightMargin, ... Declared before rightMargin = Convert.ToInt32(e.MarginBounds.Right) topMargin = Convert.ToInt32(e.MarginBounds.Top) bottomMargin = Convert.ToInt32(e.MarginBounds.Bottom) InvoiceWidth = Convert.ToInt32(e.MarginBounds.Width) InvoiceHeight = Convert.ToInt32(e.MarginBounds.Height) ' Draw Invoice Head SetInvoiceHead(e.Graphics) End Sub
- 设置
Font
和Color
Dim InvTitleFont As Font = New Font("Arial", 24, FontStyle.Regular) Dim HeadBrush As SolidBrush = New SolidBrush(Color.Blue)
-
设置
Font Height
和Font Width
以及坐标,然后使用DrawString
方法Private Sub SetInvoiceHead(ByVal g As Graphics) 'Invoice title: Dim InvTitle As String = "International Food Company" 'Title Font: Dim InvTitleFont As Font = New Font("Arial", 24, FontStyle.Regular) 'Title Color: Dim HeadBrush As SolidBrush = New SolidBrush(Color.Blue) 'Title Height: Dim InvTitleHeight As Integer = Convert.ToInt32(InvTitleFont.GetHeight(g)) 'Title Length: Dim lenInvTitle As Integer = Convert.ToInt32(g.MeasureString_ (InvTitle, InvTitleFont).Width) 'Coordinate: Dim CurrentX As Integer = leftMargin + (InvoiceWidth - lenInvTitle) / 2 'to set the title in center Dim CurrentY As Integer = topMargin + InvTitleHeight 'draw the title: g.DrawString(InvTitle, InvTitleFont, HeadBrush, CurrentX, CurrentY) End Sub
该项目在三个窗体中有几段代码。请阅读代码,然后运行程序查看结果。您可以阅读关于
- 如何使用
PrintPreviewDialog
控件和PrintDocument
控件创建报告? - 如何绘制发票抬头?
- 如何绘制产品及其价格的表格?
- 如何计算并绘制发票总额?
如果您有任何想法或发现任何问题,请告诉我。
您可以阅读我的下一篇文章,了解如何使用 C# 打印发票。
我添加了另一个 VB.NET 2010 的项目。