c# 指定打印机(2台)打印不同的内容 不指定

Else , 2009/05/07 11:51 , 代码片段,演示也有 , Comments(0) , Reads(1373) , Via Original
这个忙了三天,终于测试好了
  /// <summary>
        ///  打印
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_print_Click(object sender, EventArgs e)
        {


            Microsoft.Office.Interop.Word.Application app = null;
            Microsoft.Office.Interop.Word.Document doc = null;
            object missing = System.Reflection.Missing.Value;
            object templateFile = Application.StartupPath + @"\列印的格式.doc";


            Microsoft.Office.Interop.Word.Application app2 = null;
            Microsoft.Office.Interop.Word.Document doc2 = null;

            object templateFile2 = Application.StartupPath + @"\地址條.doc";


            try
            {
                    app = new Microsoft.Office.Interop.Word.ApplicationClass();
                    doc = app.Documents.Add(ref templateFile, ref missing, ref missing, ref missing);

                    app2 = new Microsoft.Office.Interop.Word.ApplicationClass();
                    doc2 = app2.Documents.Add(ref templateFile2, ref missing, ref missing, ref missing);


                    try
                    {
                       //doc1
                        
                        
                        foreach (Microsoft.Office.Interop.Word.Bookmark bm in doc.Bookmarks)
                        {
                            bm.Select();
                            string item = bm.Name;
                            if (item.Equals("chuhuo"))
                            {
                                bm.Range.Text = txt_qiaohuo.Text;
                            }
                            else if (item.Equals("table1"))
                            {
                               // bm.Range.Tables.Add(Microsoft.Office.Interop.Word.Selection.Range, 5, 6, ref missing, ref missing);

                            }

                        }


                       // doc2


                        OP_POCollect_Barcode opcb = new OP_POCollect_Barcode();


                        DataSet ds = opcb.ShowInfo(txt_qiaohuo.Text);

                        this.text = "打列中"+ds.Tables[0].Rows.Count;

                        if (ds.Tables[0].Rows.Count > 0)
                        {

                        foreach (Microsoft.Office.Interop.Word.Bookmark bm in doc2.Bookmarks)
                        {
                            
                        
                                bm.Select();
                                string item = bm.Name;
                                if (item.Equals("address"))
                                {
                                    //地址
                                    bm.Range.Text = ds.Tables[0].Rows[0]["city"].ToString() + ds.Tables[0].Rows[0]["area"].ToString() + ds.Tables[0].Rows[0]["CntAddress"].ToString();
                                }
                                else if (item.Equals("shou"))
                                {
                                   //收件人
                                    bm.Range.Text = ds.Tables[0].Rows[0]["CustName"].ToString();
                                    
                                }

                                else if (item.Equals("lianluo"))
                                {
                                   //"联络人"
                                    bm.Range.Text = ds.Tables[0].Rows[0]["BuyMam"].ToString();;

                                }

                                else if (item.Equals("hao"))
                                {
                                    bm.Range.Text = "查货单号";

                                }

                                else if (item.Equals("haomatiao"))
                                {
                                    bm.Range.Text = "条码";

                                }

                                else if (item.Equals("jiechuhuanluhao"))
                                {
                                    bm.Range.Text = "单号";

                                }

                                else if (item.Equals("jianshu"))
                                {

                                    // "件数"
                                    bm.Range.Text = ds.Tables[0].Rows[0]["PackageQty"].ToString();;

                                }

                                else if (item.Equals("wuliu"))
                                {
                                    //物流
                                    bm.Range.Text = ds.Tables[0].Rows[0]["DeliryOrg"].ToString();

                                }
                            
                            
                            }


                          

                        }


                     TF_BLN tf_bln = new TF_BLN();
                     DataSet tbds = tf_bln.GetList("  BL_NO='" + txt_qiaohuo.Text.Trim() + "'");

                     int tbrowcount = tbds.Tables[0].Rows.Count;

                    object count = 2;
                    object WdLine = Microsoft.Office.Interop.Word.WdUnits.wdLine;//换一行;
                    app.Selection.MoveDown(ref WdLine, ref count, ref missing);//移动焦点
                    app.Selection.TypeParagraph();

                    //文档中创建表格------------------
                    Microsoft.Office.Interop.Word.Table newTable = doc.Tables.Add(app.Selection.Range, tbrowcount + 1, 6, ref missing, ref missing);
                    //设置表格样式
                    newTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
                   newTable.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;


                    newTable.Columns[1].Width = 60f;
                    newTable.Columns[2].Width = 80f;
                    newTable.Columns[3].Width = 80f;
                    newTable.Columns[4].Width = 100f;
                    newTable.Columns[5].Width = 80f;
                    newTable.Columns[6].Width = 80f;
                    //填充表格内容
                    newTable.Cell(1, 1).Range.Text = "品號";
                    newTable.Cell(1, 2).Range.Text = "品名";
                    newTable.Cell(1, 3).Range.Text = "訂購數量";
                    newTable.Cell(1, 4).Range.Text = "已出貨數量";
                    newTable.Cell(1, 5).Range.Text = "本次出貨";
                    newTable.Cell(1, 6).Range.Text = "備註";

                    for (int i = 0; i < tbrowcount; i++)
                    {
                        newTable.Cell(i + 2, 1).Range.Text = tbds.Tables[0].Rows[i]["prd_no"].ToString();
                        newTable.Cell(i + 2, 2).Range.Text = tbds.Tables[0].Rows[i]["prd_name"].ToString();
                        newTable.Cell(i + 2, 3).Range.Text = "";
                        newTable.Cell(i + 2, 4).Range.Text = "已出貨數量";
                        newTable.Cell(i + 2, 5).Range.Text = tbds.Tables[0].Rows[i]["qty"].ToString();
                        newTable.Cell(i + 2, 6).Range.Text = tbds.Tables[0].Rows[i]["rem"].ToString();
                    
                    }




                }
                catch
                {
                }
              
              

                //打印
               doc.Application.ActivePrinter = System.Configuration.ConfigurationManager.AppSettings["print1"];
               doc.PrintOut(ref missing, ref missing, ref missing, ref missing,
                     ref missing, ref missing, ref missing, ref missing, ref missing,
                     ref missing, ref missing, ref missing, ref missing, ref missing,
                     ref missing, ref missing, ref missing, ref missing);



               doc2.Application.ActivePrinter = System.Configuration.ConfigurationManager.AppSettings["print2"];
               doc2.PrintOut(ref missing, ref missing, ref missing, ref missing,
             ref missing, ref missing, ref missing, ref missing, ref missing,
             ref missing, ref missing, ref missing, ref missing, ref missing,
             ref missing, ref missing, ref missing, ref missing);



            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message, this.Text);
            }
            //销毁word进程
            finally
            {
                object saveChange = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
                if (doc != null)
                    doc.Close(ref saveChange, ref missing, ref missing);
                if (app != null)
                    app.Quit(ref missing, ref missing, ref missing);

                object saveChange2 = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
                if (doc2 != null)
                    doc2.Close(ref saveChange, ref missing, ref missing);
                if (app2 != null)
                    app2.Quit(ref missing, ref missing, ref missing);
            }








            //第二個打印













            
            
            
            
            
            
            
            ////关联打印预处理代码
            //printDocument.BeginPrint += new PrintEventHandler(printDocument_BeginPrint);
            ////关联打印代码
            //printDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintPage);

            ////定义打印对话框
            //PrintDialog printDialog = new PrintDialog();

            ////获得用户输入
            //DialogResult dr = printDialog.ShowDialog();

            ////如果确认则打印
            //if (dr == DialogResult.OK)
            //{
            //    printDocument.Print();
            //}
            //else
            //{
            //    return;
            //}

        }
Tags: , , , ,
Add a comment

Nickname

Site URI

Email

Enable HTML Enable UBB Enable Emots Hidden Remember [Login] [Register]