C#word模板导出(合同导出)

来源:互联网 发布:淘宝二手ps4可靠吗 编辑:程序博客网 时间:2024/05/01 04:53
privatevoid export_Click(object sender, EventArgs e)  //word模板导出
       {
           template = toolStripComboBox1.Text;
           if(template == "")
           {
               MessageBox.Show("请选择导出模板!");
               return;
           }
           FolderBrowserDialog dlg =new FolderBrowserDialog();
           if(dlg.ShowDialog() == DialogResult.OK)
           {
               sourcefile = System.Windows.Forms.Application.StartupPath +@"\" + toolStripComboBox1.Text + ".doc";
               targetPath = dlg.SelectedPath;
  
               backgroundWorker1.RunWorkerAsync();
               cmd.ShowOpaqueLayer(this, 150,true);
           }
       }
  
       publicoverride void doWork(object sender, DoWorkEventArgs e)
       {
           exportWord("");
       }
  
       privatevoid exportWord(string file)
       {
  
           intnum = 0;
           for(int i = 0; i < dt1.Rows.Count; i++)
           {
  
               backgroundWorker1.ReportProgress(num + 1,null);
               stringtargetfile;
               if(file.Length == 0)
               {
                    targetfile = targetPath +@"\" + "农村土地承包经营权委托流转协议.doc";
               }else {
                   targetfile = file;
                   sourcefile = System.Windows.Forms.Application.StartupPath +@"\" + toolStripComboBox1.Text + ".doc";
               }
               System.IO.File.Copy(sourcefile, targetfile,true);
               System.Object oMissing = System.Reflection.Missing.Value;
               Microsoft.Office.Interop.Word._Application WordApp =new Microsoft.Office.Interop.Word.Application();
               WordApp.Visible =false;
               objectfilename = targetfile;
               Microsoft.Office.Interop.Word._Document WordDoc = WordApp.Documents.Open(reffilename,
                   refoMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                   refoMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                   refoMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
  
               //int excelcode = Convert.ToInt32(code.Substring(14, 3));//户编号
  
  
               stringhzxm = dt1.Rows[i]["户主姓名"].ToString();//承包方
               stringzjhm = dt1.Rows[i]["证件号码"].ToString();
               stringlxdh = dt1.Rows[i]["联系电话"].ToString();
               stringdz = dt1.Rows[i]["住址"].ToString();
               stringwtr = dt1.Rows[i]["委托人"].ToString();
               stringwtrzjhm = dt1.Rows[i]["委托人证件号码"].ToString();
               stringwtrlxdh = dt1.Rows[i]["委托人联系电话"].ToString();
               stringwtrdz = dt1.Rows[i]["委托人地址"].ToString();
  
               //合同编号
               objectagreementCode = "户主姓名";
               if(WordDoc.Bookmarks.Exists("户主姓名"))
               {
                   WordDoc.Bookmarks.get_Item(refagreementCode).Range.Text = hzxm;
  
               }
  
  
  
               objectfarmerAddress = "证件号码";
               if(WordDoc.Bookmarks.Exists("证件号码"))
               {
                   WordDoc.Bookmarks.get_Item(reffarmerAddress).Range.Text = zjhm;
               }
  
               objectfarmerFullName = "地址";
               if(WordDoc.Bookmarks.Exists("地址"))
               {
                   WordDoc.Bookmarks.get_Item(reffarmerFullName).Range.Text = dz;
               }
  
               objectfarmerDelegateName = "联系电话";
               if(WordDoc.Bookmarks.Exists("联系电话"))
               {
                   WordDoc.Bookmarks.get_Item(reffarmerDelegateName).Range.Text = lxdh;
               }
  
                 
               objectregionFullName = "委托人";
               if(WordDoc.Bookmarks.Exists("委托人"))
               {
                   WordDoc.Bookmarks.get_Item(refregionFullName).Range.Text = wtr;
               }
  
                 
               objectfarmerFarmerIDCode = "委托人证件号码";
               if(WordDoc.Bookmarks.Exists("委托人证件号码"))
               {
                   WordDoc.Bookmarks.get_Item(reffarmerFarmerIDCode).Range.Text = wtrzjhm;
               }
  
               objectfarmerFarmerIDCode1 = "委托人地址";
               if(WordDoc.Bookmarks.Exists("委托人地址"))
               {
                   WordDoc.Bookmarks.get_Item(reffarmerFarmerIDCode1).Range.Text = wtrdz;
               }
  
                 
               objectfarmerHouseholdPhone = "委托人联系电话";
               if(WordDoc.Bookmarks.Exists("委托人联系电话"))
               {
                   WordDoc.Bookmarks.get_Item(reffarmerHouseholdPhone).Range.Text = wtrlxdh;
               }
  
               //保存
               WordDoc.Save();
               WordDoc.Close(refoMissing, ref oMissing, ref oMissing);
               WordApp.Quit(refoMissing, ref oMissing, ref oMissing);
               WordApp =null;
               num++;
           }
       }
  
       publicoverride void Progress(objectsender, ProgressChangedEventArgs e)
       {
  
       }
  
       publicoverride void End(object sender, AsyncCompletedEventArgs e)
       {
           //终止WORD进程
           System.Diagnostics.Process[] myProcesses = System.Diagnostics.Process.GetProcesses();
           foreach(System.Diagnostics.Process myProcess in myProcesses)
           {
               if("WINWORD" == myProcess.ProcessName) myProcess.Kill();
           }
           cmd.HideOpaqueLayer();
           MessageBox.Show("导出完成");
       }
 
  privatevoid toolStripButton2_Click(object sender, EventArgs e)  //调用word中的打印预览实现对word的模板打印
        {
            stringtemplate1 = toolStripComboBox1.Text;
            if(template1 == "")
            {
                MessageBox.Show("请选择导出模板!");
                return;
            }
            stringdirectoryPath = System.Windows.Forms.Application.StartupPath + @"\tempWord\";
            DirectoryInfo dirInfo =new DirectoryInfo(directoryPath);
            if(!dirInfo.Exists)
            {
                Directory.CreateDirectory(directoryPath);
            }
            stringfilePath = directoryPath + "农村土地承包经营权委托流转协议.doc";
 
            FileInfo fileinfo =new FileInfo(filePath);
 
            if(fileinfo.Exists)
            {
                string[] files = Directory.GetFiles(fileinfo.DirectoryName);
                foreach(string str in files)
                {
                    if(System.IO.Path.GetFileNameWithoutExtension(str) == "农村土地承包经营权委托流转协议")
                    {
                        FileUtil.delFile(str);
                    }
                }
            }
 
            //Stream myStream = fileinfo.OpenWrite();
 
            //StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
            try
            {
                exportWord(filePath);
                //sw.Close();
                //myStream.Close();
 
 
                Microsoft.Office.Interop.Word.Application xlsApp =new Microsoft.Office.Interop.Word.Application();
                //xlsApp.Visible = true;
 
                Microsoft.Office.Interop.Word.Documents xlsWbs = xlsApp.Documents;
                Microsoft.Office.Interop.Word.Document xlsWb = xlsWbs.Open(filePath, Missing.Value,false, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                //Microsoft.Office.Interop.Word.Documents  xlsWs = (Microsoft.Office.Interop.Word.Document)xlsWb.Worksheets[1];
 
                //使excel可见
                xlsApp.Visible =true;
                //预览
                xlsWb.PrintPreview();
 
                //退出,并释放资源
                //xlsApp.DisplayAlerts = false;
 
                //xlsWs = null;
                //xlsWb = null;
                //xlsApp.Quit();
                //xlsApp = null;
            }
 
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                //sw.Close();
                //myStream.Close();
                //终止EXCEL进程
                System.Diagnostics.Process[] myProcesses = System.Diagnostics.Process.GetProcesses();
                foreach(System.Diagnostics.Process myProcess in myProcesses)
                {
                    if("WORD" == myProcess.ProcessName) myProcess.Kill();
                }
            }
        }

现在word合同中需要系统填写的地方加入书签(插入--书签) 书签名与object farmerFarmerIDCode1 = "委托人地址"一样
0 0