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 ...
随机推荐
- Java 调用Dll
Java 中怎么能调用到dll中的函数呢? 关键是java中生的本地函数名參数和dll中的本地函数名參数一模一样. 这个程序是java中调用dll中的求和函数. 一,java代码部分操作 1.新建pr ...
- Windows如何打包Qt程序
很多Qt爱好者想发布自己的Qt软件,但却发现在其他没有安装Qt SDK的机器上无法运行,这就是本文想要说明的问题.现在网上大部分软件都要发布自己开发的应用程序,都会打包到exe文件中,待安装完exe文 ...
- Service 如何知道caller
重写Binder的onTransact方法 1 you need to do that in Binder#onTransact method, this is a good place for ...
- CUDA与VS2013安装
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
- windows10UWP:如何在xaml中设置控件为 public ?
windows10UWP中,由于使用页面导航,操作在不同一个页面的控件需求经常遇到. 如果要对另一个page里面的控件进行操作,那么这个控件必须设置为 public .在 xaml 设置控件的方法是: ...
- html_entity_decode() 函数
html_entity_decode() 函数 定义和用法 The html_entity_decode() function converts HTML entities to characte ...
- hihocoder 北大网络赛 E.
给一个1000个点的多边形(从某个点依次按照外形给出每个节点),这个多边形不一定是凸多边形 再给一个圆,问这个多边形与圆相交区域的周长 我们将这个问题分成两个部分,第一部分是求线段在圆内的长度,第二部 ...
- Session 原理
Session天天用,但是你真的理解了么? 今天遇到了这个问题,于是研究了一下.要解决这个问题,首先就要明白一些Session的机理.Session在服务器是以散列表形式存在的,我们都知道Sessio ...
- linux工具问题,tail -f 失效
最近发现一个很奇怪问题: tail -f 不能实时的输出日志
- (转)PHP下编码转换函数mb_convert_encoding与iconv的使用说明
之--http://www.jb51.net/article/21451.htm mb_convert_encoding这个函数是用来转换编码的.原来一直对程序编码这一概念不理解,不过现在好像有点开窍 ...