C# 打印制式表单

 

 


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace Print1
{
    public partial class Form1 : Form
    {
        internal static TextBox t1;
        internal static TextBox t2;
        internal static TextBox t3;

        public Form1()
        {
            InitializeComponent();
            t1 = textBox1;
            t2 = textBox2;
            t3 = textBox3;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Equals("")) {
                textBox1.Text = "0.00";
            }
            textBox1.Text = Regex.Replace(textBox1.Text, @"^[0]+", "");

 //           MessageBox.Show(textBox1.Text);

            int i;
            if (textBox1.Text.Contains("."))
            {
                i = textBox1.Text.IndexOf(".");
                if (i == 0) { 
                    textBox1.Text = "0" + textBox1.Text;
 //                   MessageBox.Show(textBox1.Text);
                }
                if (textBox1.Text.Length - i == 1)
                {
                    textBox1.Text += "00";
 //                   MessageBox.Show(textBox1.Text);
                }
                else if (textBox1.Text.Length - i == 2)
                {
                    textBox1.Text += "0";
 //                   MessageBox.Show(textBox1.Text);
                }
            }
            else
            {
                textBox1.Text += ".00";
 //               MessageBox.Show(textBox1.Text);
            }



            if (CheckDecimal.checkDecimal(textBox1.Text))
            {
                textBox1.Text = "¥" + textBox1.Text.Replace(".", "");
                Form2 f2 = new Form2();
                f2.Show();
            }
            else {
                MessageBox.Show("无效数字!");
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.Size = new Size(355, 45);
        }

        private void button4_Click(object sender, EventArgs e)
        {
           Application.Exit();
          //  this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
            textBox6.Text = "";
            textBox7.Text = "";
            textBox8.Text = "";
            textBox9.Text = "";
            textBox10.Text = "";
            textBox11.Text = "";
            textBox12.Text = "";
            textBox13.Text = "";
            textBox14.Text = "";
            textBox15.Text = "";
            textBox1.Text = "";
            textBox1.Text = "";

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Equals(""))
            {
                textBox1.Text = "0.00";
            }
            textBox1.Text = Regex.Replace(textBox1.Text, @"^[0]+", "");

            //           MessageBox.Show(textBox1.Text);

            int i;
            if (textBox1.Text.Contains("."))
            {
                i = textBox1.Text.IndexOf(".");
                if (i == 0)
                {
                    textBox1.Text = "0" + textBox1.Text;
                    //                   MessageBox.Show(textBox1.Text);
                }
                if (textBox1.Text.Length - i == 1)
                {
                    textBox1.Text += "00";
                    //                   MessageBox.Show(textBox1.Text);
                }
                else if (textBox1.Text.Length - i == 2)
                {
                    textBox1.Text += "0";
                    //                   MessageBox.Show(textBox1.Text);
                }
            }
            else
            {
                textBox1.Text += ".00";
                //               MessageBox.Show(textBox1.Text);
            }



            if (CheckDecimal.checkDecimal(textBox1.Text))
            {
                textBox1.Text = "¥" + textBox1.Text.Replace(".", "");
                Form2 f2 = new Form2();
                f2.myprint();
            }
            else
            {
                MessageBox.Show("无效数字!");
            }
        }

    }

    public class CheckDecimal {
        public static bool checkDecimal(string s) {;
            if(Regex.IsMatch(s, @"^[\d]{0,11}[.]{0,1}[\d]{0,2}$") || Regex.IsMatch(s, @"^[\s]*$")) {
                return true;
            } else {
                return false;
            }
        }
    }
}

 

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NPOI.HSSF.UserModel;
using System.IO;
using System.Diagnostics;

namespace Print1
{
    public partial class Form2 : Form
    {

        Label[] JE = new Label[14];
        public Form2()
        {
            InitializeComponent();
            this.printDocument1.OriginAtMargins = true;//启用页边距
            this.pageSetupDialog1.EnableMetric = true; //以毫米为单位
            this.printDocument1.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);



        }

        private void Form2_Load(object sender, EventArgs e)
        {

            //           try
            //          {

            String filename = @".\data.xls";
            FileStream file = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            HSSFWorkbook wb = new HSSFWorkbook(file);
            file.Close();
            HSSFSheet sh = (HSSFSheet)wb.GetSheet("Sheet1");

            label6.Text = sh.GetRow(1).GetCell(1).StringCellValue;
            label7.Text = sh.GetRow(2).GetCell(1).StringCellValue;

 //           MessageBox.Show(label6.Text);
  //          MessageBox.Show(label7.Text);

 //           FileStream fileout = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite);
   //         wb.Write(fileout);
  //          fileout.Close();

            //          }
            //          catch
            //          {
            //              MessageBox.Show("数据文件错误!");
            //         }

            JE[0] = label9;
            JE[1] = label10;
            JE[2] = label11;
            JE[3] = label12; 
            JE[4] = label13;
            JE[5] = label14;
            JE[6] = label15;
            JE[7] = label16; 
            JE[8] = label17;
            JE[9] = label18; 
            JE[10] = label19;
            JE[11] = label20;
            JE[12] = label21;
            JE[13] = label22;
            int i, j = 0;;
            for (i = 0; i < 14; i++) {
                JE[i].Text = "";
            }

            i = Form1.t1.Text.Length - 1;

            char c;
            for (; i >= 0; i--) {
                c = Form1.t1.Text[i];
                JE[j++].Text = (string)c.ToString();
            }


            label1.Text = DateTime.Now.Year + "   " + DateTime.Now.Month + "   " + DateTime.Now.Day;
            label2.Text = Form1.t2.Text;
            label3.Text = Form1.t3.Text;

            int x = Convert.ToInt32(label6.Text), y = Convert.ToInt32(label7.Text);
            panel2.Location = new System.Drawing.Point(x, y);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            panel2.BackgroundImage = null;
            this.pageSetupDialog1.ShowDialog();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            panel2.BackgroundImage = null;
            this.printPreviewDialog1.ShowDialog();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            panel2.BackgroundImage = null;
            if (this.printDialog1.ShowDialog() == DialogResult.OK) {
                this.printDocument1.Print();
            }
        }

        internal void myprint() {
            panel2.BackgroundImage = null;
 //           if (this.printDialog1.ShowDialog() == DialogResult.OK)
            {
                this.printDocument1.Print();
            }       
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            //打印内容 为 整个Form
            //Image myFormImage;
            //myFormImage = new Bitmap(this.Width, this.Height);
            //Graphics g = Graphics.FromImage(myFormImage);
            //g.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, this.Size);
            //e.Graphics.DrawImage(myFormImage, 0, 0);

            //打印内容 为 局部的 this.groupBox1
            Bitmap _NewBitmap = new Bitmap(panel1.Width, panel1.Height);
            panel1.DrawToBitmap(_NewBitmap, new Rectangle(0, 0, _NewBitmap.Width, _NewBitmap.Height));
            e.Graphics.DrawImage(_NewBitmap, 0, 0, _NewBitmap.Width, _NewBitmap.Height);

            //打印内容 为 自定义文本内容 
            //          Font font = new Font("宋体", 12);
            //          Brush bru = Brushes.Blue;
            //       for (int i = 1; i <= 5; i++)
            {
                //              e.Graphics.DrawString("Hello world ", font, bru, i * 20, i * 20);
            }
        }

        private void panel2_MouseEnter(object sender, EventArgs e)
        {
            Bitmap b = new Bitmap(@".\ckpt.jpg");
           panel2.BackgroundImage = b;
;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            int x = Convert.ToInt32(label6.Text), y = Convert.ToInt32(label7.Text);
            x += 1;
            panel2.Location = new Point(x, y);
            label6.Text = panel2.Location.X.ToString();
        }



        //∧∨<>

        private void button5_Click(object sender, EventArgs e)
        {
            int x = Convert.ToInt32(label6.Text), y = Convert.ToInt32(label7.Text);
            x -= 1;
            panel2.Location = new Point(x, y);
            label6.Text = panel2.Location.X.ToString();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            int x = Convert.ToInt32(label6.Text), y = Convert.ToInt32(label7.Text);
            y += 1;
            panel2.Location = new System.Drawing.Point(x, y);
            label7.Text = panel2.Location.Y.ToString();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            int x = Convert.ToInt32(label6.Text), y = Convert.ToInt32(label7.Text);
            y -= 1;
            panel2.Location = new System.Drawing.Point(x, y);
            label7.Text = panel2.Location.Y.ToString();
        }

        private void button8_Click(object sender, EventArgs e)
        {
 //           try
  //          {

                String filename = @".\data.xls";
                FileStream file = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                HSSFWorkbook wb = new HSSFWorkbook(file);
                file.Close();
                HSSFSheet sh = (HSSFSheet)wb.GetSheet("Sheet1");

                sh.GetRow(1).GetCell(1).SetCellValue(label6.Text);
                sh.GetRow(2).GetCell(1).SetCellValue(label7.Text);
               
                FileStream fileout = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                wb.Write(fileout);
                fileout.Close();

  //          }
  //          catch
  //          {
  //              MessageBox.Show("数据文件错误!");
   //         }
        }

  
    }
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_39410618

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值