我是靠谱客的博主 从容小鸽子,最近开发中收集的这篇文章主要介绍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 = "委托人地址"一样

最后

以上就是从容小鸽子为你收集整理的C#word模板导出(合同导出)的全部内容,希望文章能够帮你解决C#word模板导出(合同导出)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(44)

评论列表共有 0 条评论

立即
投稿
返回
顶部