Csharp 简单操作Word模板文件
1.创建一个模板的Word文档 Doc1.dot 内容为:
To: <Name>
Sub:<Subject>
Website is ok geovindu 涂聚文好样的 work.
CEo
<Datetime>
Thanks.
2.程序操作代码为:
/// <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);
}
Csharp 简单操作Word模板文件的更多相关文章
- C#操作word模板插入文字、图片及表格详细步骤
c#操作word模板插入文字.图片及表格 1.建立word模板文件 person.dot用书签 标示相关字段的填充位置 2.建立web应用程序 加入Microsoft.Office.Interop.W ...
- [转]C#操作word模板插入文字、图片及表格详细步骤
c#操作word模板插入文字.图片及表格 1.建立word模板文件 person.dot用书签 标示相关字段的填充位置 2.建立web应用程序 加入Microsoft.Office.Interop.W ...
- 使用NPOI按照word模板文件生成新的word文件
/// <summary> /// 按照word模板文件 生成新word文件 /// </summary> /// <param name="tempFile& ...
- SpringBoot 集成 FreeMarker 导出 Word 模板文件(底部附源码)
思路解说 word 模板文件(doc 或 docx 文件)另存为 xml 文件 将后缀 xml 改成 html:大部分文档会改成 ftl(FreeMarker 的后缀名),因为 word 文件另存为 ...
- C#操作word类文件
最近频繁操作Word文档,写了很多word的操作代码及方法,虽然已经有很多关于word的操作类了,自己还是进行了一下整合: 1.通过模板创建新文件 2.在书签处插入值 3.插入表格 4.合并单元格 5 ...
- Chimm.Excel —— 使用Java 操作 excel 模板文件生成 excel 文档
Chimm.Excel -- 设置模板,填充数据,就完事儿了~ _____ _ _ _____ _ / __ \ | (_) | ___| | | | / \/ |__ _ _ __ ___ _ __ ...
- C# 操作word 模板 值 替换
1.引用 aspose.words dll 2.word 使用doc 3.给word 模板中添加要替换位置的 书签 .引用 aspose.words dll .word 使用doc .给word ...
- java代码操作word模板并生成PDF
这个博客自己现在没时间写,等后面有时间了,自己再写. 这中需求是在实际的项目开发中是会经常遇到的. 下面我们先从简单入手一步一步开始. 1.首先,使用word创建一个6行两列的表格. 点击插入-6行2 ...
- 关于Docx动态控制word模板文件的数据
博客:https://www.cnblogs.com/24klr/ github: https://github.com/luoruiemail/Dynamic_Word_Web 参考资料:https ...
随机推荐
- my.cnf已经存在,影响安装--mysql
Found existing config file ./my.cnf on the system. Because this file might be in use, it was not rep ...
- 解决Shockwave flash在chrome浏览器上崩溃的问题
越来越多的人開始使用chrome浏览器,非常多用户都遇到过flash崩溃的问题,有时候重新启动chrome能够解决,有时候会导致无法用chrome打开不论什么站点上的不论什么flash.这个问题非常少 ...
- 虚拟化技术性能总结:Zones, KVM, Xen
[译]虚拟化技术性能总结:Zones, KVM, Xen 时间 2014-04-29 16:52:44 Babyfacer_陈晓炜 原文 http://blog.csdn.net/babyface ...
- Java序列化之Serializable
Java的序列化流程如下: Java的反序列化流程如下: 注意:并不是所有类都需要进行序列化,主要原因有两个 1)安全问题.Java中有的类属于敏感类,此类的对象数据不便对外公开,而序列化的对象数据很 ...
- 安装Visual Studio 2013 中文社区版
Visual Studio 2013 免费了,我收到邮件后,立即从邮件的下载连接安装了 Visual Studio Community 2013 with Update 4 . 安装后几天没打开,今天 ...
- oracle 异常管理
命名的系统异常 产生原因 access_into_null 未定义对象 CASE_NOT_FOUND CASE 中若未包含相应的 WHEN ,并且没有设置 ELSE ...
- python下载IGS观测数据
最近在学习GPS数据软件处理,经常需要下载数据练习,反复去网站上很麻烦,于是就写了一个小小的爬虫,用的是韩国的服务器,使用了python中的ftplib库实现的 今天稍微改了一下代码,可以选择卫星系统 ...
- Ant工具
Ant工具 Ant是一种基于Java的build工具.理论上来说,它有些类似于(Unix)C中的make ,但没有make的缺陷.目前的最新版本为:Ant 1.9.4[1] . Ant的概念 当一 ...
- sp_addlinkedserver的一些操作
sp_addlinkedserver 创建一个链接的服务器,使其允许对分布式的.针对 OLE DB 数据源的异类查询进行访问.在使用 sp_addlinkedserver 创建链接的服务器之后,此服务 ...
- oracle数组学习资料
--oracle数组,所谓数组就是 字段的 个数,数组应该很有用 --可变数组 declare type v_ar is varray(10) of varchar2(30); my_ar v ...