Csharp 简单操作Word模板文件

1.创建一个模板的Word文档  Doc1.dot 内容为:

To: <Name>

Sub:<Subject>

 

Website is ok geovindu 涂聚文好样的 work.

CEo

                         <Datetime>

Thanks.

 

2.程序操作代码为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/// <summary>
 /// 簡單操作Word模板文件
 /// 塗聚文 2011-11-03
 /// 缔友计算机信息技术有限公司
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void button2_Click(object sender, EventArgs e)
 {
     DateTime startTime = DateTime.Now;
     DateTime endTime = DateTime.Now;
     System.Random srd = new Random();
     int srdName = srd.Next(1000);
     string newfile = path + DateTime.Now.ToString("yyyyMMddhhmmss") + srdName.ToString()+".doc"; //新文件名        
     try
     {
          
         startTime = DateTime.Now;  
         //killprocess("winword");
         //  復制模板文件
         File.Copy(path + fileName, newfile, true);//fileName = "Doc1.doc";//模板文件
         
         object missing = Missing.Value;
          
         Word.Application wordApp = new Word.ApplicationClass();
         endTime = DateTime.Now;
          
         Word.Document aDoc = null;
         
         object file = newfile;//新文件替換               
         if (File.Exists((string)file))//判斷新文件存在否
         {
             object readOnly = false;
             object isVisible = false;                   
             wordApp.Visible = false;                   
             aDoc = wordApp.Documents.Open(ref file, ref missing,
             ref readOnly, ref missing, ref missing, ref missing,
             ref missing, ref missing, ref missing, ref missing,
             ref missing, ref isVisible, ref missing, ref missing,
             ref missing, ref missing);
             aDoc.Activate();                  
             this.FindAndReplace(wordApp, "<Datetime>", this.dateTimePicker1.Text);
             this.FindAndReplace(wordApp, "<Name>", this.textBoxName.Text.Trim());
             this.FindAndReplace(wordApp, "<Subject>",this.textBoxSubject.Text.Trim());                   
             aDoc.Save();
         }
         else
             MessageBox.Show("File does not exist.","No File", MessageBoxButtons.OK,MessageBoxIcon.Information);
        // killprocess("winword");
     }
     catch (Exception)
     {
         MessageBox.Show("Error in process.", "Internal Error",
         MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
     finally
     {
         killprocess(startTime, endTime, "winword");
         GC.Collect();
         GC.WaitForPendingFinalizers();
     }
 }
 /// <summary>
 /// 關閉進程 塗聚文 2011-11-03
 /// </summary>
 /// <param name="pro"></param>
 public void killprocess(DateTime start, DateTime end, string ProcessName)
 {
     //foreach (Process p in Process.GetProcessesByName(pro))
     //{
     //    if (!p.HasExited)
     //    {
     //        p.Kill();
     //    }
     //}
         Process[] myProcesses;
         DateTime ProStartTime;
         myProcesses = Process.GetProcessesByName(ProcessName);
 
         //因为暂时没有想到得到Excel进程ID的方法,所以只能判断进程启动时间
         foreach (Process myProcess in myProcesses)
         {
             ProStartTime = myProcess.StartTime;
             //myProcess.Id
 
             if (start <= ProStartTime && ProStartTime <= end)
             {
                 myProcess.Kill();
             }
         }
 
 
 }
 /// <summary>
 /// 找查替換內容 塗聚文 2011-11-03
 /// </summary>
 /// <param name="wordApp"></param>
 /// <param name="findText"></param>
 /// <param name="replaceText"></param>
 private void FindAndReplace(Word.Application wordApp,object findText, object replaceText)
 {
     object matchCase = true;
     object matchWholeWord = true;
     object matchWildCards = false;
     object matchSoundsLike = false;
     object matchAllWordForms = false;
     object forward = true;
     object format = false;
     object matchKashida = false;
     object matchDiacritics = false;
     object matchAlefHamza = false;
     object matchControl = false;
     object read_only = false;
     object visible = true;
     object replace = 2;
     object wrap = 1;
     wordApp.Selection.Find.Execute(ref findText, ref matchCase,
         ref matchWholeWord, ref matchWildCards, ref matchSoundsLike,
         ref matchAllWordForms, ref forward, ref wrap, ref format,
         ref replaceText, ref replace, ref matchKashida,
                 ref matchDiacritics,
         ref matchAlefHamza, ref matchControl);
 }

 

posted @   ®Geovin Du Dream Park™  阅读(2686)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2010-11-03 CSS three column layout
2009-11-03 Reference List between HTTP_ACCEPT_LANGUAGE Servervariable value, Locale ID (LCID) and Language区域化设置
< 2011年11月 >
30 31 1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 1 2 3
4 5 6 7 8 9 10
点击右上角即可分享
微信分享提示