Csharp 简单操作Word模板文件

时间:2023-01-09 14:10:21

原文: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&num;操作word模板插入文字、图片及表格详细步骤

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

  2. &lbrack;转&rsqb;C&num;操作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. C&num;操作word类文件

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

  5. Chimm&period;Excel —— 使用Java 操作 excel 模板文件生成 excel 文档

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

  6. C&num; 操作word 模板 值 替换

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

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

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

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

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

  9. C&num;简单操作app&period;config文件

    即将操作的app.config文件内容如下 <?xml version="1.0" encoding="utf-8"?> <configura ...

随机推荐

  1. PostgreSQL除法保留小数

    select round(1::numeric/4::numeric,3);结果为0.25 select round( cast ( 1 as numeric )/ cast( 4 as numeri ...

  2. VS2010 支持 CSS3

    在安装Standards Update for VS2010 SP1后,VS2010中没有CSS3.0问题,以下是我的解决方法 1.首先去官网下载 CSS 3 Intellisense Schema ...

  3. (转)CAS &lpar;4&rpar; —— CAS浏览器SSO访问顺序图详解(CAS Web Flow Diagram by Example)

    CAS (4) —— CAS浏览器SSO访问顺序图详解(CAS Web Flow Diagram by Example) tomcat版本: tomcat-8.0.29 jdk版本: jdk1.8.0 ...

  4. UVa 1394 约瑟夫问题的变形

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  5. XGPush集成(信鸽集成)

    #import "AppDelegate.h" #import "XGPush.h" #import "XGSetting.h" #defi ...

  6. UVA 11402 - Ahoy&comma; Pirates&excl;&lpar;段树&rpar;

    UVA 11402 - Ahoy, Pirates! 题目链接 题意:总的来说意思就是给一个01串,然后有3种操作 1.把一个区间变成1 2.把一个区间变成0 3.把一个区间翻转(0变1,1变0) 思 ...

  7. 带你轻松玩转Git--瞬间创建本地仓库

    在上一篇文章中我们对版本控制有了一个比较宏观的了解,同时也能够看到Git 所处在的历史地位.并且对版本控制系统的体系进行了一个宏观的对比,貌似让读者看起来挺复杂的样子. 笔者将会尽可能的简单向大家分享 ...

  8. Explain of Interaction Operators in UML&quest;

    来源于:EA 中的 Interaction Operators Enterprise Architect User Guide Operator Action alt Divide up intera ...

  9. 0-创建scott示例数据

    CREATE TABLE dept (  deptno INT PRIMARY KEY,  dname VARCHAR(14),  loc VARCHAR(13) );   INSERT INTO d ...

  10. 枚举特性FlagsAttribute的用法

    先看官方的解释:指示可以将枚举作为位域(即一组标志)处理. 看起来并不好理解,到底什么是作为位域处理? 其实说的通俗点就是用二进制的表示方式来处理数学集合概念中关于集合的或与非等运算方法. 有什么用 ...