Code Bye

c# 标签打印

c# 标签打印
写个小程序,用于打印标签,如图
但每次都少打了最后一条,就退纸了。  是什么问题啊
另外,每次预览都有之前预览的信息存在,怎么清除呢?
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Data.OleDb; 
using System.Drawing; 
using System.Drawing.Printing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Threading; 
using System.Xml; 
using System.IO; 
namespace 标签打印 
{ 
    public partial class Form1 : Form 
    { 
        int i = 0; 
        int j = 0; 
        
        public Form1() 
        { 
            InitializeComponent(); 
            
        } 
        private void button1_Click(object sender, EventArgs e) 
        { 
            //打开EXCEL| 
            //OpenFileDialog file = new OpenFileDialog(); 
            openFileDialog1.Filter = "xls文件(*.xls)|*.xls|xlsx文件(*.xlsx)|*.xlsx||"; 
            if (openFileDialog1.ShowDialog() == DialogResult.OK) 
            { 
                string confstring = openFileDialog1.FileName; 
                string strScource = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + confstring + ";Extended Properties=Excel 8.0"; 
                OleDbConnection conn = new OleDbConnection(strScource); 
                string sqlstring = @"select * from [Sheet1$]"; 
                OleDbDataAdapter adaper = new OleDbDataAdapter(sqlstring, conn); 
                DataSet da = new DataSet(); 
                adaper.Fill(da); 
                this.dataGridView1.DataSource = da.Tables[0]; 
            } 
            //数据导入结束,设置下列宽 
            this.dataGridView1.Columns[4].Width = 150; 
            this.dataGridView1.Columns[5].Width = 180; 
        } 
        private void Form1_Load(object sender, EventArgs e) 
        { 
            this.dataGridView1.AllowUserToAddRows = false; //删除空白条 
        } 
        private void button5_Click(object sender, EventArgs e) 
        { 
            this.Close(); 
        } 
        private void Form1_FormClosing(object sender, FormClosingEventArgs e) 
        { 
            if (MessageBox.Show("确认退出吗", "信息提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) 
                e.Cancel = true; 
        } 
        private void button2_Click(object sender, EventArgs e) 
        { 
            if (dataGridView1.Rows.Count == 0) 
            { 
                MessageBox.Show("请先导入数据!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); 
                return; 
            } 
            
            //先测试下预览 
            //一英寸=2.54厘米,这里是百分之一英寸,目前的纸张是长12厘米,宽9厘米354,472 
            this.printDocument1.DefaultPageSettings.PaperSize = new PaperSize("Custum", 354, 472);   //354,472 
            this.printDocument1.OriginAtMargins = true; 
            this.printDocument1.DefaultPageSettings.Margins.Left = 20; 
            this.printDocument1.DefaultPageSettings.Margins.Top = 1 ; 
            this.printDocument1.DefaultPageSettings.Margins.Bottom = 0; 
            this.printDocument1.PrintPage += new PrintPageEventHandler(this.my_print); 
            
            printPreviewDialog1.Document = printDocument1; 
            DialogResult result = printPreviewDialog1.ShowDialog(); 
        } 
        private void my_print(object sender, System.Drawing.Printing.PrintPageEventArgs e) 
        { 
            int aa = 0; 
            string zh; 
            string ygbh; 
            string ygname; 
            string bmname; 
            int jls; 
            int a1 = 1, a2 = 14, a3 = 14, a4 = 26; 
            int zys; //总页数 
            int ys=1; //当前页数 
            // 
            jls = this.dataGridView1.Rows.Count; 
            zys =  Convert.ToInt16 (  Math.Ceiling(Convert.ToDouble(jls) / 14.0));  //算下总页数 
            
            
            //MessageBox.Show(jls.ToString(), "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); 
            //准备遍历记录 
      // if (i ==jls) 
      //     { 
     //          MessageBox.Show("sss"); 
      //         return; 
      //      }     
                
      //     else 
      //      { 
                
                for (int m = 1; m <= 14; m++) 
                { 
                    if (i == jls) 
                    { 
                        i = 0; 
                        j = 0; 
                        
                        e.HasMorePages = false; 
                        return;   //这个是在循环体内,发现记录超了,强制出来 
                    } 
                    zh = this.dataGridView1.Rows[i].Cells[5].Value.ToString(); 
                    ygbh = this.dataGridView1.Rows[i].Cells[1].Value.ToString(); 
                    ygname = this.dataGridView1.Rows[i].Cells[2].Value.ToString(); 
                    bmname = this.dataGridView1.Rows[i].Cells[4].Value.ToString(); 
                    if (i % 2 == 0) 
                    { 
                        e.Graphics.DrawString(zh, new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black,1, a1); 
                        e.Graphics.DrawString(ygbh, new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, 1, a2); 
                        e.Graphics.DrawString(ygname, new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, 56, a3);  //(左右,上下) 
                        e.Graphics.DrawString(bmname, new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, 1, a4); 
                        e.Graphics.DrawString(i.ToString(), new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, 120, a4); 
                        j = j + 1; 
                        i = i + 1; 
                    } 
                    else 
                    { 
                        e.Graphics.DrawString(zh, new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, 165, a1); 
                        e.Graphics.DrawString(ygbh, new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, 165, a2); 
                        e.Graphics.DrawString(ygname, new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, 235, a3);  //(左右,上下) 
                        e.Graphics.DrawString(bmname, new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, 165, a4); 
                        e.Graphics.DrawString(i.ToString (), new Font(new FontFamily("宋体"), 9), System.Drawing.Brushes.Black, 265, a4); 
                        a1 = a1 + 66; 
                        a2 = a2 + 66; 
                        a3 = a3 + 66; 
                        a4 = a4 + 66; 
                        j = j + 1; 
                        i = i + 1; 
                    } 
                } 
      //     } 
       /***     if (j== 14)   //打了14条,换 页 
                { 
                     //打开多页属性 
                    
               //    MessageBox.Show(zys.ToString(), "11111信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); 
               //    MessageBox.Show(ys.ToString(), "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); 
                    ys = ys + 1; 
                    j = 0;  //清零 
                    if (ys == zys) 
                    { 
                        i = 0; 
                        e.HasMorePages = false; //打关闭多页属性 
                       //return; 
                      
                    } 
                    else 
                    { 
                       e.HasMorePages = true; 
                       MessageBox.Show(ys.ToString ()); 
                      
                    
                      
                    } 
                } 
               ***/ 
              //MessageBox.Show(j.ToString()); 
      /***        if (j==14) 
              { 
                  j = 0; 
                  MessageBox.Show(i.ToString()); 
                  if (i ==jls) 
                  { 
                      
                      i = 0; 
                      e.HasMorePages = false; 
                      return; 
                  } 
                     else 
                
                  { 
                      //  MessageBox.Show("ddddddddddd"); 
                       e.HasMorePages = true; 
                      
                   } 
                    
               } 
        
     ****/ 
                //MessageBox.Show(i.ToString()); 
                if (i == jls) 
                { 
                    i = 0; 
                    e.HasMorePages = false; 
                    return; 
                } 
          
                
            
            
                if (j == 14) 
                { 
                    j = 0; 
                    e.HasMorePages = true; 
                    
                } 
              
        } 
        private void button4_Click(object sender, EventArgs e) 
        { 
           /// MessageBox.Show(Math.Ceiling().ToString(), "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); 
        } 
    } 
}

解决方案

30

这样的不科学,要成卷的就不会这样了

5

检查windows打印设置,选中默认打印机,从属性进入

5

应该是打印机纸张设置问题,按一下送纸键看看能不能正确出一张纸。还有打印机应该是碳带打印机吧?假如是的用这种两列的纸容易测试错误的。

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明c# 标签打印