close
文章出處

  
  1 using System;
2 using System.Data;
3 using System.Drawing;
4 using System.Drawing.Printing;
5 using System.Windows.Forms;
6
7
8 namespace InvoiceSystem{
9
10 public partial class Printer : Form
11 {
12 private DataGridView invoiceGrid;
13 private int rowCount = 0; //datagridview行數
14 private int colCount = 0; //datagridview列數
15 private int x = 0; //一個datagridview單元格寬度
16 private int y = 0; //當前行行間距
17 int i = 0; //判斷行數for循環的起始值
18 private int rowGap = 60; //行間距
19 private int leftMargin = 50; //正文到左面的左邊距
20 private Font font = new Font("Arial", 10); //正文字體
21 private Font headingFont = new Font("Arial", 11, FontStyle.Underline); //標題字體
22 private Font captionFont = new Font("Arial", 10, FontStyle.Bold);
23 private Brush brush = new SolidBrush(Color.Black);
24 private string cellValue = string.Empty; //保存datagridview一個單元格里的內容
25
26
27 public Printer(DataGridView invoiceGrid)
28 {
29 InitializeComponent();
30 this.invoiceGrid = invoiceGrid; //從外部傳入一個datagridview引用
31
32 }
33
34
35 /// <summary>
36 /// 打印按鈕
37 /// </summary>
38 /// <param name="sender"></param>
39 /// <param name="e"></param>
40 private void button1_Click(object sender, EventArgs e)
41 {
42
43 printDocument1.DocumentName = "發票列表"; //打印的文檔文件名
44 PaperSize ps=new PaperSize("16開",724,1024); //自定義紙張大小
45 //ps.RawKind = 9; //設置為A4
46 printDocument1.DefaultPageSettings.PaperSize = ps;
47 printDocument1.DefaultPageSettings.Landscape = true; //使用橫向打印
48
49 printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage); //設置函數printDocument1_PrintPage為打印事件處理函數
50 printDialog1.Document = printDocument1;
51 printPreviewDialog1.Document = printDocument1;
52
53 if (printDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK && printPreviewDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
54 {
55
56 printDocument1.Print();
57
58
59 }
60 }
61
62
63 private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
64 {
65
66 rowCount = invoiceGrid.Rows.Count - 1;
67 colCount = invoiceGrid.ColumnCount;
68
69 //打印標題
70 y += rowGap;
71 x = leftMargin;
72 for (int j = 0; j < colCount; j++)
73 {
74 if (invoiceGrid.Columns[j].Width>0)
75 {
76 cellValue = invoiceGrid.Columns[j].HeaderText;
77 e.Graphics.FillRectangle(new SolidBrush(Color.LightGray), x, y, invoiceGrid.Columns[j].Width, rowGap); //將單元格的背景設置為灰色
78 e.Graphics.DrawRectangle(Pens.Black, x, y, invoiceGrid.Columns[j].Width, rowGap); //畫出一個單元格
79 e.Graphics.DrawString(cellValue, headingFont, brush, x, y); //將datagridview標題里的單元格里的內容填入剛才畫出的單元格
80 x += invoiceGrid.Columns[j].Width; //寬度向后移一個datagridview單元格寬度的單位
81 }
82 }
83
84 //打印所有行
85 for (; i < rowCount; i++)
86 {
87 y += rowGap;
88 x = leftMargin;
89
90 for (int j = 0; j < colCount; j++)
91 {
92 if (invoiceGrid.Columns[j].Width>0)
93 {
94 cellValue = invoiceGrid.Rows[i].Cells[j].Value.ToString();
95 e.Graphics.DrawRectangle(Pens.Black, x, y, invoiceGrid.Columns[j].Width, rowGap);
96 e.Graphics.DrawString(cellValue, font, brush, x, y);
97 x += invoiceGrid.Columns[j].Width;
98 }
99 }
100
101 if (y > e.PageBounds.Height) //e.PageBounds.Height表示頁邊距的高度
102 {
103 //允許多頁打印
104 y = 0;
105 e.HasMorePages = true; //如果還有沒有打印的就再執行一次print()函數把余下的打印出來
106 i++;
107 return;
108
109 }
110
111 }
112 y += rowGap;
113
114
115 //底部填充空格
116 for (int j = 0; j < colCount; j++)
117 {
118 e.Graphics.DrawString(" ", font, brush, x, y);
119 }
120 i = 0;
121 e.HasMorePages = false;
122 }
123 }
124 }

  效果就是貼出的圖片的樣子


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜

    AutoPoster 發表在 痞客邦 留言(0) 人氣()