原文: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模板文件的更多相关文章

  1. C#操作word模板插入文字、图片及表格详细步骤

    c#操作word模板插入文字.图片及表格 1.建立word模板文件 person.dot用书签 标示相关字段的填充位置 2.建立web应用程序 加入Microsoft.Office.Interop.W ...

  2. [转]C#操作word模板插入文字、图片及表格详细步骤

    c#操作word模板插入文字.图片及表格 1.建立word模板文件 person.dot用书签 标示相关字段的填充位置 2.建立web应用程序 加入Microsoft.Office.Interop.W ...

  3. 使用NPOI按照word模板文件生成新的word文件

    /// <summary> /// 按照word模板文件 生成新word文件 /// </summary> /// <param name="tempFile& ...

  4. SpringBoot 集成 FreeMarker 导出 Word 模板文件(底部附源码)

    思路解说 word 模板文件(doc 或 docx 文件)另存为 xml 文件 将后缀 xml 改成 html:大部分文档会改成 ftl(FreeMarker 的后缀名),因为 word 文件另存为 ...

  5. C#操作word类文件

    最近频繁操作Word文档,写了很多word的操作代码及方法,虽然已经有很多关于word的操作类了,自己还是进行了一下整合: 1.通过模板创建新文件 2.在书签处插入值 3.插入表格 4.合并单元格 5 ...

  6. Chimm.Excel —— 使用Java 操作 excel 模板文件生成 excel 文档

    Chimm.Excel -- 设置模板,填充数据,就完事儿了~ _____ _ _ _____ _ / __ \ | (_) | ___| | | | / \/ |__ _ _ __ ___ _ __ ...

  7. C# 操作word 模板 值 替换

    1.引用 aspose.words   dll 2.word 使用doc 3.给word 模板中添加要替换位置的 书签 .引用 aspose.words dll .word 使用doc .给word ...

  8. java代码操作word模板并生成PDF

    这个博客自己现在没时间写,等后面有时间了,自己再写. 这中需求是在实际的项目开发中是会经常遇到的. 下面我们先从简单入手一步一步开始. 1.首先,使用word创建一个6行两列的表格. 点击插入-6行2 ...

  9. 关于Docx动态控制word模板文件的数据

    博客:https://www.cnblogs.com/24klr/ github: https://github.com/luoruiemail/Dynamic_Word_Web 参考资料:https ...

随机推荐

  1. Shell脚本编程——了解你的Linux系统必须掌握的20个命令

    要想详细了解你的Linux系统,为系统评估和性能调化提供准确的信息,那么,你会经常用到这几组命令. 一. 系统信息     1. 查看内核版本.编译主机.编译器版本和编译时间的信息    cat /p ...

  2. json-lib-2.4-jdk15.jar maven

    最近自己将一个web项目装换到使用mevan自动管理. 遇到了一个json包导入的问题.最终解决如下: <!-- https://mvnrepository.com/artifact/net.s ...

  3. Robolectric Test-Drive Your Android Code

    Running tests on an Android emulator or device is slow! Building, deploying, and launching the app o ...

  4. SPFA 小优化*2

    /* bzoj 2763 SPFA小优化 循环队列+SLF 顺面改掉自己之前手打qeueu的坏毛病*/ #include<iostream> #include<cstring> ...

  5. 遍历id,根据id作为条件循环查询。

    string id = "OE09924008161405102,OE36765709071405102,OE87852044171405102,OE09924008161405102&qu ...

  6. C#重载重写

    overload:重载指的是同一个类中有两个或多个名字相同但是参数不同的方法,(注:返回值不能区别函数是否重载),重载没有关键字.override:过载也称重写是指子类对父类中虚函数或抽象函数的“覆盖 ...

  7. JS escape()、encodeURI()和encodeURIComponent()的区别

    1.实例说明: var url='http://wx.jnqianle.com/content/images/冰皮月饼.jpg?name=张三丰&age=11'; console.info(w ...

  8. [转]mysql自动定时备份数据库的最佳方法-支持windows系统

    网上有很多关于window下Mysql自动备份的方法,可是真的能用的也没有几个,有些说的还非常的复杂,难以操作. 我们都知道mssql本身就自带了计划任务可以用来自动备份,可是mysql咱们要怎么样自 ...

  9. 关于DM的一点总结[ZZ]

    用IBM的IM做过一段时间的电信客户挖掘由于时间不是很长,做的挖掘模型效果还有待提高应朋友要求简单总结几点(水平有限,也希望经验丰富的朋友给些建议): 1.挖掘工具主要分商业数据产品和集成数据挖掘产品 ...

  10. TIFF6 Packbit algorithm

    “Packbits” from ISO 12369 参考TIFF 6.0 Specification,点击TIFF, Version 6.0: @Section 9: PackBits Compres ...