C#打印報表


#region  打印
        private void MyPrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            //int x = 100;
            ///*如果需要改變自己 可以在new Font(new FontFamily("黑體"),11)中的“黑體”改成自己要的字體就行了,黑體 后面的數字代表字體的大小
            // System.Drawing.Brushes.Blue , 170, 10 中的 System.Drawing.Brushes.Blue 為顏色,后面的為輸出的位置 */
            //e.Graphics.DrawString("新鄉市三月軟件公司入庫單", new Font(new FontFamily("黑體"), 11), System.Drawing.Brushes.Black, 170, 10);
            //e.Graphics.DrawString("供貨商:河南科技學院", new Font(new FontFamily("黑體"), 8), System.Drawing.Brushes.Blue, 10, 12);
            ////信息的名稱
            //e.Graphics.DrawLine(Pens.Black, 8, 30, 520, 30);
            //e.Graphics.DrawString("序號", new Font(new FontFamily("黑體"), 10), System.Drawing.Brushes.Black, 9, 35);
            //e.Graphics.DrawString("入庫編號", new Font(new FontFamily("黑體"), 10), System.Drawing.Brushes.Black, 75, 35);
            //e.Graphics.DrawString("商品名稱", new Font(new FontFamily("黑體"), 10), System.Drawing.Brushes.Black, 230, 35);
            //e.Graphics.DrawString("數量", new Font(new FontFamily("黑體"), 10), System.Drawing.Brushes.Black, 330, 35);
            //e.Graphics.DrawString("單價", new Font(new FontFamily("黑體"), 10), System.Drawing.Brushes.Black, 405, 35);
            //e.Graphics.DrawString("總金額", new Font(new FontFamily("黑體"), 10), System.Drawing.Brushes.Black, 470, 35);
            //e.Graphics.DrawLine(Pens.Black, 8, 50, 520, 50);
            ////產品信息
            //double value = 100;
            //int num = 0;
            //for (int i = 1; i <= strData.Count-1 ; i++)
            //{
            //    e.Graphics.DrawString(i.ToString(), new Font(new FontFamily("黑體"), 10), System.Drawing.Brushes.Black, 9, 55+ num);
            //    e.Graphics.DrawString("R2011-01-2016:06:35", new Font(new FontFamily("黑體"), 10), System.Drawing.Brushes.Black, 75, 55 + num);
            //    e.Graphics.DrawString(strData[i], new Font(new FontFamily("黑體"), 10), System.Drawing.Brushes.Black, 230 , 55 + num);
            //    e.Graphics.DrawString("100", new Font(new FontFamily("黑體"), 10), System.Drawing.Brushes.Black, 330, 55 + num);
            //    e.Graphics.DrawString("200.00", new Font(new FontFamily("黑體"), 10), System.Drawing.Brushes.Black, 405, 55 + num);
            //    e.Graphics.DrawString(value.ToString(), new Font(new FontFamily("黑體"), 10), System.Drawing.Brushes.Black, 470, 55 + num);
            //    num += 20;
            //    value = Convert.ToDouble(value);
            //}
            //e.Graphics.DrawString("總金額:"+value.ToString(), new Font(new FontFamily("黑體"), 10), System.Drawing.Brushes.Black,400, 70 + num);
            //e.Graphics.DrawLine(Pens.Black, 8, 200+num, 520, 200+num);
            //e.Graphics.DrawString("收貨地址:新鄉市河南科技學院信息工程學院", new Font(new FontFamily("黑體"), 8), System.Drawing.Brushes.Black, 15, 210 + num);
            //e.Graphics.DrawString("收貨人:任忌", new Font(new FontFamily("黑體"), 8), System.Drawing.Brushes.Black, 15, 230 + num);
            //e.Graphics.DrawString("電話號碼:15083128577", new Font(new FontFamily("黑體"), 8), System.Drawing.Brushes.Black, 15, 250 + num);
            //e.Graphics.DrawString("經辦人:任忌", new Font(new FontFamily("黑體"), 8), System.Drawing.Brushes.Black, 15, 270 + num);
            //e.Graphics.DrawString("服務熱線:15083128577", new Font(new FontFamily("黑體"), 8), System.Drawing.Brushes.Black, 15, 290 + num);
            //e.Graphics.DrawString("出庫時間:" + DateTime.Now.ToString(), new Font(new FontFamily("黑體"), 8), System.Drawing.Brushes.Black, 15, 310 + num);
            //第一條豎線開始30,100
            //第2條豎線坐標開始130,100
            //第三條豎線開始230,linebegin,結束230,lineend
            //第四條豎線開始340,linebegin,結束340,lineend
            //第五條豎線開始460,linebegin,結束460,lineend
            //第六條豎線開始670,linebegin,結束670,lineend
            //最后一條豎線開始790,100
            int y = 50;
            int linebegin = 0;
            int lineend = 0;
            int end = 0;
            Font font = new Font("宋體", 10, FontStyle.Regular);
            Pen title_pen = new Pen(Color.Black, 1);
            e.Graphics.DrawString("專家抽取列表", new Font("宋體", 20, FontStyle.Bold), Brushes.Black, 270, y);
            y += 80;
            e.Graphics.DrawLine(title_pen, 20, y, 690, y);//第一條橫線
            e.Graphics.DrawString("項目名稱", font, Brushes.Black, (20 + (90 - GetWidth("項目名稱", font)) / 2 - 5), y + 10);
            e.Graphics.DrawString("1", font, Brushes.Black, (110 + (570 - GetWidth("1", font)) / 2 - 5), y + 10);
            y += 40;
            e.Graphics.DrawLine(title_pen, 20, y, 690, y);//第二條橫線
            e.Graphics.DrawString("招標單位", font, Brushes.Black, (20 + (90 - GetWidth("招標單位", font)) / 2 - 5), y + 10);
            e.Graphics.DrawString("2", font, Brushes.Black, (110 + (570 - GetWidth("2", font)) / 2 - 5), y + 10);
            y += 40;
            e.Graphics.DrawLine(title_pen, 20, y, 690, y);//第三條橫線
            e.Graphics.DrawString("抽取時間", font, Brushes.Black, (20 + (90 - GetWidth("抽取時間", font)) / 2 - 5), y + 10);
            e.Graphics.DrawString("3", font, Brushes.Black, (110 + (570 - GetWidth("3", font)) / 2 - 5), y + 10);
            y += 40;
            e.Graphics.DrawLine(title_pen, 20, y, 690, y);//第四條橫線
            linebegin = y;
            //開始畫列名
            e.Graphics.DrawString("專業", font, Brushes.Black, (20 + (90 - GetWidth("專業", font)) / 2 - 5), y + 10);
            e.Graphics.DrawString("姓名", font, Brushes.Black, (110 + (70 - GetWidth("姓名", font)) / 2 - 5), y + 10);
            e.Graphics.DrawString("固定電話", font, Brushes.Black, (180 + (90 - GetWidth("固定電話", font)) / 2 - 5), y + 10);
            e.Graphics.DrawString("手機", font, Brushes.Black, (270 + (110 - GetWidth("手機", font)) / 2 - 5), y + 10);
            e.Graphics.DrawString("工作單位", font, Brushes.Black, (380 + (200 - GetWidth("工作單位", font)) / 2 - 5), y + 10);
            e.Graphics.DrawString("職稱", font, Brushes.Black, (580 + (110 - GetWidth("職稱", font)) / 2 - 5), y + 10);
            y += 40;
            e.Graphics.DrawLine(title_pen, 20, y, 690, y);//第五條橫線
            //開始畫table數據
            foreach (DataGridViewRow dr in this.dataGridView1.Rows)
            {
                e.Graphics.DrawString(dr.Cells[1].Value.ToString(), font, Brushes.Black, (20 + (90 - GetWidth(dr.Cells[1].Value.ToString(), font)) / 2 - 5), y + 10);
                e.Graphics.DrawString(dr.Cells[2].Value.ToString(), font, Brushes.Black, (110 + (70 - GetWidth(dr.Cells[2].Value.ToString(), font)) / 2 - 5), y + 10);
                int phone = GetWidth(dr.Cells[3].Value.ToString(), font);
                if (phone <= 90)
                {
                    e.Graphics.DrawString(dr.Cells[3].Value.ToString(), font, Brushes.Black, (180 + (90 - phone) / 2 - 5), y + 10);
                }
                else
                {
                    string _phone = dr.Cells[3].Value.ToString();
                    _phone = _phone.Substring(0, 9) + "…";
                    phone = GetWidth(_phone, font);
                    e.Graphics.DrawString(_phone, font, Brushes.Black, (180 + (90 - phone) / 2 - 5), y + 10);
                }
                int mobile = GetWidth(dr.Cells[4].Value.ToString(), font);
                if (mobile <= 100)
                {
                    e.Graphics.DrawString(dr.Cells[4].Value.ToString(), font, Brushes.Black, (270 + (110 - mobile) / 2 - 5), y + 10);
                }
                else
                {
                    string _mobile = dr.Cells[4].Value.ToString();
                    _mobile = _mobile.Substring(0, 11) + "…";
                    phone = GetWidth(_mobile, font);
                    e.Graphics.DrawString(_mobile, font, Brushes.Black, (270 + (110 - phone) / 2 - 5), y + 10);
                }
                e.Graphics.DrawString(dr.Cells[5].Value.ToString(), font, Brushes.Black, (380 + (200 - GetWidth(dr.Cells[5].Value.ToString(), font)) / 2 - 5), y + 10);
                e.Graphics.DrawString(dr.Cells[6].Value.ToString(), font, Brushes.Black, (580 + (110 - GetWidth(dr.Cells[6].Value.ToString(), font)) / 2 - 5), y + 10);
                y += 40;
                e.Graphics.DrawLine(title_pen, 20, y, 690, y);
            }
            lineend = y;
            e.Graphics.DrawString("抽取人簽字", font, Brushes.Black, (20 + (90 - GetWidth("抽取人簽字", font)) / 2 - 5), y + 10);
            y += 40;
            e.Graphics.DrawLine(title_pen, 20, y, 690, y);
            e.Graphics.DrawString("監督人簽字", font, Brushes.Black, (20 + (90 - GetWidth("監督人簽字", font)) / 2 - 5), y + 10);
            y += 40;
            e.Graphics.DrawLine(title_pen, 20, y, 690, y);
            end = y;
            //開始畫豎線
            //e.Graphics.DrawLine(title_pen, 20, 130, 20, y);
            //e.Graphics.DrawLine(title_pen, 130, 130, 130, y);
            //e.Graphics.DrawLine(title_pen, 210, linebegin, 210, lineend);
            //e.Graphics.DrawLine(title_pen, 320, linebegin, 320, lineend);
            //e.Graphics.DrawLine(title_pen, 440, linebegin, 440, lineend);
            //e.Graphics.DrawLine(title_pen, 670, linebegin, 670, lineend);
            //e.Graphics.DrawLine(title_pen, 800, 130, 800, y);
            e.Graphics.DrawLine(title_pen, 20, 130, 20, y);
            e.Graphics.DrawLine(title_pen, 110, 130, 110, y);
            e.Graphics.DrawLine(title_pen, 180, linebegin, 180, lineend);
            e.Graphics.DrawLine(title_pen, 270, linebegin, 270, lineend);
            e.Graphics.DrawLine(title_pen, 380, linebegin, 380, lineend);
            e.Graphics.DrawLine(title_pen, 580, linebegin, 580, lineend);
            e.Graphics.DrawLine(title_pen, 690, 130, 690, y);
        }
        SpeechSynthesizer speak = new SpeechSynthesizer();
        List<string> strData = new List<string>();
        private void button2_Click(object sender, EventArgs e)
        {
            //// printDocument1 為 打印控件
            ////設置打印用的紙張 當設置為Custom的時候,可以自定義紙張的大小,還可以選擇A4,A5等常用紙型
            //this.MyPrintDocument.DefaultPageSettings.PaperSize = new PaperSize("Custom", 500, 300);
            //this.MyPrintDocument.PrintPage += new PrintPageEventHandler(this.MyPrintDocument_PrintPage);
            ////將寫好的格式給打印預覽控件以便預覽
            //printPreviewDialog1.Document = MyPrintDocument;
            ////printPreviewDialog1.PrintPreviewControl.Zoom = 3.0;
            ////顯示打印預覽
            //DialogResult result = printPreviewDialog1.ShowDialog();
            ////if (result == DialogResult.OK)
            ////    this.MyPrintDocument.Print();
            printDialog1.ShowDialog();  //設置打印文檔
            printPreviewDialog1.Document = this.MyPrintDocument;
            printPreviewDialog1.PrintPreviewControl.Zoom = 1;
            printPreviewDialog1.ClientSize = new Size(800, 800);
            printPreviewDialog1.ShowDialog();
        }
        #endregion
 private int GetWidth(string str, Font myf)
        {
            Graphics g = Graphics.FromHwnd(this.Handle);
            StringFormat sf = new StringFormat(StringFormat.GenericTypographic);
            SizeF size = g.MeasureString(str, myf, 1000, sf);
            return Convert.ToInt32(size.Width);
        }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM