C#,WPF使用word模板导出word文档

Posted 浪子心太乱

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#,WPF使用word模板导出word文档相关的知识,希望对你有一定的参考价值。

   使用word模板导出word文档,首先需要在word模板中插入书签:

根据创建的书签名和位置,将需要写入的内容插入到word文件中。

需要引用  Microsoft.Office.Interop.Word;在添加引用-程序集中搜索可以找到。

 

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Telerik.Windows.Controls;

namespace LawsRegulationDate.LawenforceManagement.DocumentFile
{
    /// <summary>
    /// Interaction logic for HomePage.xaml
    /// </summary>
    public partial class HomePage
    {
        public HomePage()
        {
            InitializeComponent();
        }

        private void 行政执法首页_Click(object sender, RoutedEventArgs e)
        {
            //模板文件
            string urlstring = System.Windows.Forms.Application.StartupPath + "\\\\File\\\\WordTemplate\\\\行政执法案卷(首页).docx";
            if (string.IsNullOrEmpty(urlstring) || !File.Exists(urlstring))
            {
                Message_Box.Show("【行政执法案卷(首页)】模板不存在!");
                return;
            }
            else
            {
                string fileName = string.Empty;
                System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();
                fbd.ShowDialog();
                if (fbd.SelectedPath != string.Empty)
                {
                    fileName = fbd.SelectedPath + "\\\\行政执法案卷(首页)" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".docx";
                    WordEstablish(urlstring, fileName);
                }
                else
                {
                    Message_Box.Show("选择导出位置");
                    return;
                }


            }
        }
        /// <summary>
        /// 调用生产方法
        /// </summary>
        /// <param name="urlstring">模板文件</param>
        /// <param name="fileName">新文件</param>
        public void WordEstablish(string urlstring, string fileName)
        {
            try
            {
                #region 声明参数
                Dictionary<string, string> DList = new Dictionary<string, string>();
                DList.Add("案由", 案由.Text);
                DList.Add("处理结果", 处理结果.Text);
                DList.Add("立案年", LAYear.Text);
                DList.Add("立案月", LAMonth.Text);
                DList.Add("立案日", LADay.Text);
                DList.Add("结案年", JAYear.Text);
                DList.Add("结案月", JAMonth.Text);
                DList.Add("结案日", JADay.Text);
                DList.Add("", JianText.Text);
                DList.Add("", YeText.Text);
                DList.Add("档案号", GDText.Text);
                DList.Add("承办人", CBText.Text);
                DList.Add("归档年", GDYear.Text);
                DList.Add("归档月", GDMonth.Text);
                DList.Add("归档日", GDDay.Text);
                DList.Add("保存期限", BCYear.Text);
                #endregion
                if (WordClass.ExportWord(urlstring, fileName, DList))
                {
                    MessageBox.Show("生成\'" + fileName + "\'成功!");
                }
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.ToString());
                return;
            }
        }
    }
}

 

WordClass.cs类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Word = Microsoft.Office.Interop.Word;
using System.Collections.Generic;
using System.IO;
namespace LawsRegulationDate
{
    public class WordClass
    {
        /// <summary>
        /// 模板生成word
        /// </summary>
        /// <param name="templateFile">模板文件</param>
        /// <param name="fileName">新文件</param>
        /// <param name="myDictionary">参数数组</param>
        public static bool ExportWord(string templateFile, string fileName, Dictionary<string, string> myDictionary)
        {
            try
            {
                //生成word程序对象
                Word.Application app = new Word.Application();
                //模板文件
                string TemplateFile = templateFile;
                //模板文件拷贝到新文件
                File.Copy(TemplateFile, fileName);
                //生成documnet对象
                Word._Document doc = new Word.Document();
                object Obj_FileName = fileName;
                object Visible = false;
                object ReadOnly = false;
                object missing = System.Reflection.Missing.Value;
                //打开文件
                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();
                #region 声明参数
                if (myDictionary.Count > 0)
                {
                    object what = Word.WdGoToItem.wdGoToBookmark;
                    object WordMarkName;
                    foreach (var item in myDictionary)
                    {
                        WordMarkName = item.Key;
                        doc.ActiveWindow.Selection.GoTo(ref what, ref missing, ref missing, ref WordMarkName);//光标转到书签的位置
                        doc.ActiveWindow.Selection.TypeText(item.Value);//插入的内容,插入位置是word模板中书签定位的位置
                        doc.ActiveWindow.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;//设置当前定位书签位置插入内容的格式
                    }
                }
                #endregion
                //输出完毕后关闭doc对象
                object IsSave = true;
                doc.Close(ref IsSave, ref missing, ref missing);
                return true;
            }
            catch
            {

                return false;
            }
        }
    }
}

 

以上是关于C#,WPF使用word模板导出word文档的主要内容,如果未能解决你的问题,请参考以下文章

java freemarker模板 实现word文件导出

java freemarker模板 实现word文件导出

java freemarker 实现word文件导出

java freemarker 实现word文件导出

java如何根据word模板生成word文档

Java按照word模板导出下载文档