如何使用 C# 打印发票?






4.58/5 (29投票s)
这是一个使用 C# 打印发票的尝试。
引言
如何使用 C# 打印发票?这是一个使用 C# 打印发票的尝试。
您还可以阅读另一篇文章 (如何使用 VB.NET 打印发票?)。
我的项目包含三个窗体
frmInvoice
:将DataGrid
与Northwind
数据库文件中的所有Orders
绑定frmInput
:选择您想要打印其Invoice
的Order
frmOrder
:在DataGrid
上显示Invoice
,然后您可以预览或打印发票作为Report
此外,我们需要三个类用于打印
System.Windows.Forms.PrintDialog
System.Windows.Forms.PrintPreviewDialog
System.Drawing.Printing.PrintDocument
当然,您可以使用任何数据库文件代替 Northwind.mdb 并更改我的代码以连接到您的数据库文件,您还可以更改我的 SQL 字符串以将 DataGrid
与数据绑定。
Using the Code
将 frmInvoice
窗体中的 DataGrid
与所有 Orders
绑定
// following lines to connect with access database file 'Northwind.mdb'
string MyPass = "";
string MyDataFile = Application.StartupPath + @"\DataFile\Northwind.mdb";
string strCon = @"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:
string strCon = @"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:
string InvSql = "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
OleDbDataAdapter datAdp = new OleDbDataAdapter(InvSql, strCon);
// create a command builder
OleDbCommandBuilder cBuilder = new OleDbCommandBuilder(datAdp);
// create a DataTable to hold the query results
DataTable dTable = new DataTable;
// fill the DataTable
datAdp.Fill(dTable);
// set DataSource of DataGrid
datGrid.DataSource = dTable;
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
将 frmOrder
窗体中的 DataGrid
与一个 Order
绑定
// Declare 'InvoiceOrder' in 'clsGlobal' class,
//'InvoiceOrder' is the number of Order which you select:
int intOrder = int.Parse(clsGlobal.InvoiceOrder);
string MyDataFile = Application.StartupPath + @"\DataFile\Northwind.mdb";
string MyPass = "";
dtring strCon = "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
OleDbDataAdapter datAdp = new OleDbDataAdapter(InvSql, strCon);
// create a command builder
OleDbCommandBuilder cBuilder = new OleDbCommandBuilder(datAdp);
// create a DataTable to hold the query results
DataTable dTable = new DataTable;
// fill the DataTable
datAdp.Fill(dTable);
// Create a TableStyle to format Datagrid columns.
ordGrid.TableStyles.Clear();
DataGridTableStyle tableStyle = new DataGridTableStyle;
foreach (DataColumn dc in dTable.Columns)
{
DataGridTextBoxColumn txtColumn = new DataGridTextBoxColumn;
txtColumn.MappingName = dc.ColumnName;
txtColumn.HeaderText = dc.Caption;
switch (dc.ColumnName.ToString())
{
case "ProductID": // Product ID
txtColumn.HeaderText = "Product ID";
txtColumn.Width = 60;
break;
case "ProductName": // Product Name
txtColumn.HeaderText = "Product Name";
txtColumn.Width = 110;
break;
case "UnitPrice": // Unit Price
txtColumn.HeaderText = "Unit Price";
txtColumn.Format = "0.00";
txtColumn.Alignment = HorizontalAlignment.Right;
txtColumn.Width = 60;
break;
case "Discount": // Discount
txtColumn.HeaderText = "Discount";
txtColumn.Format = "p"; // percent
txtColumn.Alignment = HorizontalAlignment.Right;
txtColumn.Width = 60;
break;
case "Quantity": // Quantity
txtColumn.HeaderText = "Quantity";
txtColumn.Alignment = HorizontalAlignment.Right;
txtColumn.Width = 50;
break;
case "ExtendedPrice": // Extended Price
txtColumn.HeaderText = "Extended Price";
txtColumn.Format = "0.00";
txtColumn.Alignment = HorizontalAlignment.Right;
txtColumn.Width = 90;
break;
}
tableStyle.GridColumnStyles.Add(txtColumn);
}
tableStyle.MappingName = dTable.TableName;
ordGrid.TableStyles.Add(tableStyle);
// set DataSource of DataGrid
ordGrid.DataSource = dTable.DefaultView;
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
声明并初始化三个用于打印的实例
private System.Windows.Forms.PrintDialog prnDialog;
private System.Windows.Forms.PrintPreviewDialog prnPreview;
private System.Drawing.Printing.PrintDocument prnDocument;
this.prnDialog = new System.Windows.Forms.PrintDialog();
this.prnPreview = new System.Windows.Forms.PrintPreviewDialog();
this.prnDocument = new System.Drawing.Printing.PrintDocument();
// the Event of 'PrintPage'
prnDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler
(prnDocument_PrintPage);
要在报告上绘制一些内容(如线条或文本)
- 获取左边距、右边距、上边距、下边距、报告宽度和报告高度
// Result of the Event 'PrintPage' private void prnDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { leftMargin = (int)e.MarginBounds.Left; // leftMargin, rightMargin, // ... Declared before rightMargin = (int)e.MarginBounds.Right; topMargin = (int)e.MarginBounds.Top; bottomMargin = (int)e.MarginBounds.Bottom; InvoiceWidth = (int)e.MarginBounds.Width; InvoiceHeight = (int)e.MarginBounds.Height; // Draw Invoice Head SetInvoiceHead(e.Graphics); }
- 设置
Font
和Color
Font InvTitleFont = new Font("Arial", 24, FontStyle.Regular); SolidBrush HeadBrush = new SolidBrush(Color.Blue);
- 设置
Font
高度和Font
宽度以及坐标,然后使用DrawString
方法private void SetInvoiceHead (Graphics g) { //Invoice title: string InvTitle = "International Food Company"; //Title Font: Font InvTitleFont = new Font("Arial", 24, FontStyle.Regular); //Title Color: SolidBrush HeadBrush = new SolidBrush(Color.Blue); //Title Height: int InvTitleHeight = (int)(InvTitleFont.GetHeight(g)); //Title Length: int lenInvTitle = (int)g.MeasureString(InvTitle, InvTitleFont).Width; //Coordinate: int CurrentX = leftMargin + (InvoiceWidth - lenInvTitle) / 2; //to set the title in center int CurrentY = topMargin + InvTitleHeight; //draw the title: g.DrawString(InvTitle, InvTitleFont, HeadBrush, CurrentX, CurrentY); }
该项目在三个窗体中有几段代码,请阅读代码,然后运行程序查看结果。
您可以阅读关于
- 如何使用
PrintPreviewDialog
控件和PrintDocument
控件创建报告? - 如何绘制
Invoice
标题? - 如何绘制产品及其价格的表格?
- 如何计算并绘制
Invoice
总计?
如果您有任何想法或发现任何问题,请告诉我。
您可以阅读 我的另一篇文章,了解如何使用 VB.NET 打印发票。