c#使用Word模版实现打印
2011-10-14 09:08
281 查看
c# Word 模版 打印
最近一个项目需要用到C# Word打印,经过几天折腾,终于得到一个颇为完美的方案。新建一个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#中实现打印功能(C#中PrintDialog,PrintDocument的使用) (转)
- 在C#中实现打印功能(C#中PrintDialog,PrintDocument的使用)
- C#中使用GDI+与PrintDocument实现打印
- 在C#中实现打印功能(C#中PrintDialog,PrintDocument的使用)
- C#使用指定打印机打印Word,Excel等Office文件和打印PDF文件的代码
- C#中使用[GDI+]+PrintDocument实现打印
- 在C#中使用PrintDialog可以很方便的实现程序的打印功能。
- 使用jsp实现word、excel格式报表打印
- 在C#中实现打印功能(C#中PrintDialog,PrintDocument的使用)
- 在C#中实现打印功能(C#中PrintDialog,PrintDocument的使用)
- C#不使用ReportViewer直接打印Report文件(rdlc)的实现代码
- C#实现对Word、Excel等的模版定制(转载)
- C#中使用GDI+实现复杂打印
- 在C#中实现打印功能(C#中PrintDialog,PrintDocument的使用)
- 在C#中实现打印功能(C#中PrintDialog,PrintDocument的使用)实用的类
- 使用C#实现在word中插入页眉页脚的方法
- C# 简单操作word(可使用模版)
- C#使用指定打印机打印Word,Excel等Office文件和打印PDF文件的代码
- C#利用word书签实现模板打印功能
- 在 C# 中实现打印功能(C# 中 PrintDialog,PrintDocument 的使用)