c# Word 模版 打印
2013-12-20 16:31
323 查看
新建一个Word操作类
首先需要添加COM引用-------Microsoft Word 11.0 Object Library
添加命名空间-------------------using Word = Microsoft.Office.Interop.Word;
private static Word._Document wDoc = null; --word文档
private static Word._Application wApp = null; --word进程
开启一个新的word进程
Word.Application thisApplication = new Word.ApplicationClass();
wApp = thisApplication;
string tmpDocFile="D:\Documents and Settings\Administrator\桌面\tmp.doc"
object templatefile = tmpDocFile;
wDoc = wApp.Documents.Add(ref templatefile, ref missing, ref missing, ref missing); //在现有进程内打开文档
wDoc.Activate(); //当前文档置前
------------------------字符替换模块----------------------------------------------------------------------
//替换模版中的字符开始
object replaceAll = Word.WdReplace.wdReplaceAll; //替换所有
wApp.Selection.Find.ClearFormatting();
wApp.Selection.Find.Text = "#old#"; //替换的字符为#old#
wApp.Selection.Find.Format = false;
wApp.Selection.Find.Forward = true; //向前查找
wApp.Selection.Find.MatchByte = true;
wApp.Selection.Find.Wrap = Word.WdFindWrap.wdFindAsk;
wApp.Selection.Find.Replacement.ClearFormatting();
wApp.Selection.Find.Replacement.Text = "new string"; //替换后新字符
wApp.Selection.Find.Execute(
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref replaceAll, ref missing, ref missing, ref missing, ref missing);
//替换模版中的字符开始结束
-------以上模块的作用是查找模版文件中所有的字符"#old#"替换成新字符"new string"
-------如果需要替换不同的字符,循环以上模块即可
-------也可以采用网上说的建立书签那种模式,不过我认为比较麻烦,不大方便,若遇到有大量数据需要替换的模版则麻烦,这个简单
----------------------在指定位置处插入图片 ,或者替换字符为图片-----------------------------------
//一般我们要在模版内插入图片,这个位置是事先定好的,我们在模版内使用字符串“#Image#”来代替
string FileName = "c:\1.jpg";//图片所在路径
object LinkToFile = false;
object SaveWithDocument = true;
wApp.Selection.Find.ClearFormatting();
wApp.Selection.Find.Text = "#Image#";
wApp.Selection.Find.Format = false;
wApp.Selection.Find.Forward = true;
wApp.Selection.Find.MatchByte = true;
wApp.Selection.Find.Wrap = Word.WdFindWrap.wdFindAsk;
if (wApp.Selection.Find.Execute(
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing))
{
wApp.Selection.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref missing);
}
//插入图片结束 , 如果有多个图片需要替换,循环该模块即可
------------------------使用Printout方法进行打印------------------------------
object background = false; //这个很重要,否则关闭的时候会提示请等待Word打印完毕后再退出,加上这个后可以使Word所有
页面发送完毕到打印机后才执行下一步操作
wDoc.SaveAs(ref filename, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref
missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
wDoc.PrintOut(ref background, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref
missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing);
object saveOption = Word.WdSaveOptions.wdSaveChanges;
wDoc.Close(ref saveOption, ref missing, ref missing); //关闭当前文档,如果有多个模版文件进行操作,则执行完这一步后接着执行打开Word文档的方法即可
saveOption = Word.WdSaveOptions.wdDoNotSaveChanges;
wApp.Quit(ref saveOption, ref missing, ref missing); //关闭Word进程
相关文章推荐
- C# PrintDocument
- C# Graphics 字体倒置
- C#委托事件定义调用实例 .
- C#委托调用实例 .
- C# where用法
- C#中的委托和事件(续)
- C#委托事件定义调用实例
- C# 中的委托和事件
- C#引用类型与值类型的比较
- C#常用的加密算法
- c# 隐式类型 转换注意点
- C#4语法
- C# System.Drawing Graphics 画图
- C# 只启动一个实例完全解决方案
- C# 利用第三方SharpZipLib进行ZIP压缩
- C#的默认访问修饰符
- C# xml 文件的简单操作
- c#中的常用ToString()方法总结
- 【坑爹的c#】用bitmap和picturebox实现多张png的透明叠加
- 【弱弱的c#】構造函數