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

private void 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);
           }
       }
  
       public override void doWork(object sender, DoWorkEventArgs e)
       {
           exportWord("");
       }
  
       private void exportWord(string file)
       {
  
           int num = 0;
           for (int i = 0; i < dt1.Rows.Count; i++)
           {
  
               backgroundWorker1.ReportProgress(num + 1, null);
               string targetfile;
               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;
               object filename = targetfile;
               Microsoft.Office.Interop.Word._Document WordDoc = WordApp.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);
  
               //int excelcode = Convert.ToInt32(code.Substring(14, 3));//户编号
  
  
               string hzxm = dt1.Rows[i]["户主姓名"].ToString();//承包方
               string zjhm = dt1.Rows[i]["证件号码"].ToString();
               string lxdh = dt1.Rows[i]["联系电话"].ToString();
               string dz = dt1.Rows[i]["住址"].ToString();
               string wtr = dt1.Rows[i]["委托人"].ToString();
               string wtrzjhm = dt1.Rows[i]["委托人证件号码"].ToString();
               string wtrlxdh = dt1.Rows[i]["委托人联系电话"].ToString();
               string wtrdz = dt1.Rows[i]["委托人地址"].ToString();
  
               //合同编号
               object agreementCode = "户主姓名";
               if (WordDoc.Bookmarks.Exists("户主姓名"))
               {
                   WordDoc.Bookmarks.get_Item(ref agreementCode).Range.Text = hzxm;
  
               }
  
  
  
               object farmerAddress = "证件号码";
               if (WordDoc.Bookmarks.Exists("证件号码"))
               {
                   WordDoc.Bookmarks.get_Item(ref farmerAddress).Range.Text = zjhm;
               }
  
               object farmerFullName = "地址";
               if (WordDoc.Bookmarks.Exists("地址"))
               {
                   WordDoc.Bookmarks.get_Item(ref farmerFullName).Range.Text = dz;
               }
  
               object farmerDelegateName = "联系电话";
               if (WordDoc.Bookmarks.Exists("联系电话"))
               {
                   WordDoc.Bookmarks.get_Item(ref farmerDelegateName).Range.Text = lxdh;
               }
  
                 
               object regionFullName = "委托人";
               if (WordDoc.Bookmarks.Exists("委托人"))
               {
                   WordDoc.Bookmarks.get_Item(ref regionFullName).Range.Text = wtr;
               }
  
                 
               object farmerFarmerIDCode = "委托人证件号码";
               if (WordDoc.Bookmarks.Exists("委托人证件号码"))
               {
                   WordDoc.Bookmarks.get_Item(ref farmerFarmerIDCode).Range.Text = wtrzjhm;
               }
  
               object farmerFarmerIDCode1 = "委托人地址";
               if (WordDoc.Bookmarks.Exists("委托人地址"))
               {
                   WordDoc.Bookmarks.get_Item(ref farmerFarmerIDCode1).Range.Text = wtrdz;
               }
  
                 
               object farmerHouseholdPhone = "委托人联系电话";
               if (WordDoc.Bookmarks.Exists("委托人联系电话"))
               {
                   WordDoc.Bookmarks.get_Item(ref farmerHouseholdPhone).Range.Text = wtrlxdh;
               }
  
               //保存
               WordDoc.Save();
               WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
               WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
               WordApp = null;
               num++;
           }
       }
  
       public override void Progress(object sender, ProgressChangedEventArgs e)
       {
  
       }
  
       public override 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("导出完成");
       }
 
  private void toolStripButton2_Click(object sender, EventArgs e)   //调用word中的打印预览实现对word的模板打印
        {
            string template1 = toolStripComboBox1.Text;
            if (template1 == "")
            {
                MessageBox.Show("请选择导出模板!");
                return;
            }
            string directoryPath = System.Windows.Forms.Application.StartupPath + @"\tempWord\";
            DirectoryInfo dirInfo = new DirectoryInfo(directoryPath);
            if (!dirInfo.Exists)
            {
                Directory.CreateDirectory(directoryPath);
            }
            string filePath = 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 = "委托人地址"一样
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值