C# .NET根据Word模版生成Word文件,PDF文件

       C# .NET根据Word模版生成Word文件,PDF文件,完整的代码,一键运行,简单易懂,上手快,实用,生成Word 生成PDF 模板生成word 模板生成PDF,需要源码可以发邮件到[email protected],如果有学习资料视频可以分享的话,可以捎带分享给我。

后台代码:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Office.Interop.Word;

namespace Demo
{
    public partial class index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Dictionary<string, string> bookmarks = new Dictionary<string, string>();
            //=============要替换的内容====,==替换的内容==
            bookmarks.Add("ERNO_number", "报告编号");
            bookmarks.Add("task_name", "实训题目");
            bookmarks.Add("student_name", "学生姓名");
            bookmarks.Add("student_number", "考试编号");
            bookmarks.Add("group_name", "小组名称");
            bookmarks.Add("collaborator", "协作者");
            bookmarks.Add("tutor", "指导老师");
            bookmarks.Add("aim", "实验目的及要求");
            bookmarks.Add("principle", "实验原理");
            bookmarks.Add("comment", "评语");
            bookmarks.Add("score", "成绩");
            bookmarks.Add("addtime", "时间");
            GenerateWord(MapPath("~/FileDeposit/模板.docx"), MapPath("~/FileUpload/words.docx"), MapPath("~/FileUpload/pdfs.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]);//替换内容
                }
                //replace(doc, "hello", "shalv");
                //此处存储时,参数可选填,如需另外生成pdf,加入一个参数ref FileName,
                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 static 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);
        }
    }
}

word模板:

猜你喜欢

转载自blog.csdn.net/IT_0802/article/details/84075504