我现在从Excel中读取所有客户数据,然后打印成信封,数据读出来了,可不知道怎么打印。
因为不知道这个打印模板做,可以随时更改打印模板!我想过用word来做打印模板,可不知道如何实现!
请高手指点,如还有其他方法,都提出来吧!
谢谢了!

解决方案 »

  1.   

    通过Printdocument显示模板绑定数据,并打印
      

  2.   

    我晕,这个问题可以直接用word邮件合并功能实现,我给同事打请帖就这样子
      

  3.   


                //用word替换的功能可以实现打印模板的功能
                 //先将模板复制到另外的文件夹,然后替换打印.
                object oMissing = System.Reflection.Missing.Value;
                Microsoft.Office.Interop.Word._Application oWord;
                Microsoft.Office.Interop.Word._Document oDoc;
                oWord = new Microsoft.Office.Interop.Word.Application();
                oWord.Visible = false;
                object fileName = "文件路径";
                oDoc = oWord.Documents.Open(ref fileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);            object FindText, ReplaceWith, Replace;
                Replace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;                FindText = "old";//要查找的文本
                    ReplaceWith = "new";//替换文本
                    oWord.Selection.Find.Execute(ref FindText, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref ReplaceWith, ref Replace, ref oMissing, ref oMissing, ref oMissing, ref oMissing);            oDoc.Save();
                oDoc.PrintOut(ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);            if (oDoc != null)
                    oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
                if (oWord != null)
                    oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
      

  4.   

    8楼的方法可以,但现在如何实现打印预览和打印呢?
    以下是打印预览的方法,可是如何和word绑定啊?        protected virtual void ShowPrintPreviewDialog(PrintDocument printDocument)
            {
                if (printDocument == null)
                {
                    throw new Exception("关联的打印文档不能为空!");
                }            try
                {
                    //申明并实例化PrintPreviewDialog
                    PrintPreviewDialog ppDlg = new PrintPreviewDialog();                //指定打印文档
                    ppDlg.Document = printDocument;
                    //显示对话框
                    DialogResult result = ppDlg.ShowDialog();
                    if (result == DialogResult.OK)
                    {
                        //...
                    }            }
                catch (System.Drawing.Printing.InvalidPrinterException e)
                {
                    MessageBox.Show("未安装打印机,请进入系统控制面版添加打印机!", "打印", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "打印", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
      

  5.   


                //word里面就有打印预览的功能
                object oMissing = System.Reflection.Missing.Value;
                Microsoft.Office.Interop.Word._Application oWord;
                Microsoft.Office.Interop.Word._Document oDoc;
                oWord = new Microsoft.Office.Interop.Word.Application();
                oWord.Visible = true;
                object fileName = "文件路径";
                oDoc = oWord.Documents.Open(ref fileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                oDoc.PrintPreview();            
      

  6.   


     //注意这个
     oWord.Visible = true; //不要下面的
     if (oDoc != null)
         oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
     if (oWord != null)
         oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
      

  7.   


    oDoc.PrintOut(ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);为什么会弹出保存为 tiff文件的对话框?
      

  8.   

    oDoc.PrintOut
    调用打印的方法了?
      

  9.   

    打印和预览分两个不同的按钮事件写.
    ==============================
    保存为 tiff文件的对话框:是你装的虚拟打印机打印文件
      

  10.   

    必须先保存成tiff文件,再将tiff文件打印出来?
      

  11.   

    分开写,怎么写啊,打印word文档,网上不多,有很多都不行
      

  12.   

    两个按钮,一个打印的,写8楼的回复.
    一个打印预览的,写10楼的回复.
    ============================================            //如果打印预览时,文档是没替换的,那就加上下面的代码
                object FindText, ReplaceWith, Replace;
                Replace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;                FindText = "old";//要查找的文本
                    ReplaceWith = "new";//替换文本
                    oWord.Selection.Find.Execute(ref FindText, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref ReplaceWith, ref Replace, ref oMissing, ref oMissing, ref oMissing, ref oMissing);            oDoc.Save();
      

  13.   

    哎,才发现,问题出在没搜索到打印机上。
    给个判断有没有打印机的代码
    我的判断不对:string strdefaultprinter = printDocument1.PrinterSettings.PrinterName;//获取默认的打印机名
                    if (strdefaultprinter == null || strdefaultprinter == "")
                    {
                        MessageBox.Show("未安装打印机,请进入系统控制面版添加打印机!", "打印", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    foreach (string strprinter in PrinterSettings.InstalledPrinters) //在列表框中列出所有的打印机, 
                    {
                        if (strprinter == null||strprinter =="")//把默认打印机设为缺省值 
                        {
                            MessageBox.Show("未安装打印机,请进入系统控制面版添加打印机!", "打印", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return;
                        }
                        strdefaultprinter = strprinter;
                        break;
                    }