博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Csharp 简单操作Word模板文件
阅读量:5085 次
发布时间:2019-06-13

本文共 4929 字,大约阅读时间需要 16 分钟。

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

To: <Name>

Sub:<Subject>

 

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

CEo

                         <Datetime>

Thanks.

 

2.程序操作代码为:

///         /// 簡單操作Word模板文件        /// 塗聚文 2011-11-03        /// 缔友计算机信息技术有限公司        ///         ///         ///         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, "
", this.dateTimePicker1.Text); this.FindAndReplace(wordApp, "
", this.textBoxName.Text.Trim()); this.FindAndReplace(wordApp, "
",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(); } } ///
/// 關閉進程 塗聚文 2011-11-03 /// ///
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(); } } } ///
/// 找查替換內容 塗聚文 2011-11-03 /// ///
///
///
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); }

 

转载于:https://www.cnblogs.com/geovindu/archive/2011/11/03/2234981.html

你可能感兴趣的文章
来,让我们谈一谈Normalize.css
查看>>
修复nexus4由于使用完美刷机或者刷机精灵导致的底层文件受损
查看>>
java JVM
查看>>
netty的入门
查看>>
监控mysql主从同步状态是否异常
查看>>
Leetcode 675.为高尔夫比赛砍树
查看>>
Java对象
查看>>
2017-2018-1 20155226 《信息安全系统设计基础》课程总结
查看>>
个人工作总结10
查看>>
数组的默认值
查看>>
(二)联动的饼图与柱形图
查看>>
js cookie实例
查看>>
用muduo实现memcached协议的例子
查看>>
数据结构与算法-二叉排序树
查看>>
linux学习第十四天 (Linux就该这么学)找到一本不错的Linux电子书
查看>>
R语言将List转为矩阵do.call
查看>>
AS-PATH(路径属性)路由路径欺骗术
查看>>
linux命令
查看>>
arcmap配置的mxd慢的问题
查看>>
Jenkins的介绍
查看>>