• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

   使用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;
            }
        }
    }
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#读写txt文件的方法发布时间:2022-07-10
下一篇:
基于C#的socket编程的TCP异步实现发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap