Asp.net或C#使用word模板生成替换后的Word和pdf文档-总结
2015-11-10 16:57
891 查看
在企业管理项目开发中,经常会有使用给定的模板文件,以及用户提交到数据里的数据,按照一定的格式,生成指定的word和pdf文档。
在这里进行一个总结:注意:(1)要再项目中添加引用:using System.Collections.Generic; using System.IO; using Microsoft.Office.Interop.Word;(2)保证模板文件夹和文件临时文件夹的存在。<img src="https://img-blog.csdn.net/20151110170122093?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />以下是页面的代码:(全)protected void Button1_Click(object sender, EventArgs e) { Dictionary<string, string> bookmarks = new Dictionary<string, string>(); bookmarks.Add("proName", "项目名称"); bookmarks.Add("proNum", "项目编号"); GenerateWord(MapPath("~/FileTemplate/testTemplate.docx"), MapPath("~/FileTemp/temp2.docx"), MapPath("~/FileTemp/temp.pdf"), bookmarks); } /// <summary> /// 根据word模板文件导出word/pdf文件 /// </summary> /// <param name="templateFile">模板路径</param> /// <param name="fileNameWord">导出文件名称</param> /// <param name="fileNamePdf">pdf文件名称</param> /// <param name="bookmarks">模板内书签集合</param> public static void GenerateWord(string templateFile, string fileNameWord, string fileNamePdf, Dictionary<string, string> bookmarks) { Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application(); File.Copy(templateFile, fileNameWord, true); Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document(); object Obj_FileName = fileNameWord; object Visible = false; object ReadOnly = false; object missing = System.Reflection.Missing.Value; object IsSave = true; object FileName = fileNamePdf; object FileFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF; object LockComments = false; object AddToRecentFiles = true; object ReadOnlyRecommended = false; object EmbedTrueTypeFonts = false; object SaveNativePictureFormat = true; object SaveFormsData = false; object SaveAsAOCELetter = false; object Encoding = Microsoft.Office.Core.MsoEncoding.msoEncodingSimplifiedChineseGB18030; object InsertLineBreaks = false; object AllowSubstitutions = false; object LineEnding = Microsoft.Office.Interop.Word.WdLineEndingType.wdCRLF; object AddBiDiMarks = false; try { doc = app.Documents.Open(ref Obj_FileName, ref missing, ref ReadOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref Visible, ref missing, ref missing, ref missing, ref missing); doc.Activate(); foreach (string bookmarkName in bookmarks.Keys) { Replace(doc, bookmarkName, bookmarks[bookmarkName]); } doc.SaveAs(ref FileName, ref FileFormat, ref LockComments, ref missing, ref AddToRecentFiles, ref missing, ref ReadOnlyRecommended, ref EmbedTrueTypeFonts, ref SaveNativePictureFormat, ref SaveFormsData, ref SaveAsAOCELetter, ref Encoding, ref InsertLineBreaks, ref AllowSubstitutions, ref LineEnding, ref AddBiDiMarks); doc.Close(ref IsSave, ref missing, ref missing); } catch { doc.Close(ref IsSave, ref missing, ref missing); } } ///<summary> /// 在word 中查找一个字符串直接替换所需要的文本 /// </summary> /// <param name="strOldText">原文本</param> /// <param name="strNewText">新文本</param> /// <returns></returns> public void Replace(Microsoft.Office.Interop.Word.Document doc, string strOldText, string strNewText) { doc.Content.Find.Text = strOldText; object FindText, ReplaceWith, Replace;// object MissingValue = Type.Missing; FindText = strOldText;//要查找的文本 ReplaceWith = strNewText;//替换文本 Replace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll; /*wdReplaceAll - 替换找到的所有项。 * wdReplaceNone - 不替换找到的任何项。 * wdReplaceOne - 替换找到的第一项。 * */ doc.Content.Find.ClearFormatting();//移除Find的搜索文本和段落格式设置 doc.Content.Find.Execute( ref FindText, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue, ref ReplaceWith, ref Replace, ref MissingValue, ref MissingValue, ref MissingValue, ref MissingValue); }
相关文章推荐
- 分享微信开发Html5轻游戏中的几个坑
- c#调用COM组件
- 星外ASP.Net的安全设置相关说明
- C#实现把指定数据写入串口
- C#动态创建button的方法
- C#中抽象方法与虚拟方法的区别
- c#中虚函数的相关使用方法
- C#使用加边法计算行列式的值
- C#实现多线程的同步方法实例分析
- C#中尾递归的使用、优化及编译器优化
- C#实现子窗体与父窗体通信方法实例总结
- C#通用邮件发送类分享
- C#中this的用法集锦
- C#.NET获取拨号连接的宽带连接方法
- C#异步绑定数据实现方法
- C#实现AddRange为数组添加多个元素的方法
- C#中Equality和Identity浅析
- C#生成饼形图及添加文字说明实例代码
- C#判等对象是否相等的方法汇总
- C#简单的向量用法实例教程