您的位置:首页 > 编程语言 > ASP

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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  asp.net word pdf dictionary c#