推荐文章

Winform 全屏显示

Winform 全屏显示

Winform程序无边框、置顶、全屏显示
WPF之Image控件

WPF之Image控件

Image控件加载图片包括加载动态图片,加载静态图片两种方式。加载动态图片通过生成一个BitmapImage,创建该对象后,赋给Image的Source即可。加载的形式:BitmapImage myBitmapImage =new BitmapImage();myBitmapImage.BeginInit();取得数据库存储的图片字段,MSSQL的Image类型Byte[] mybyte = Re
wpf list<T>与ObservableCollection<T>的区别

wpf list<T>与ObservableCollection<T>的区别

List《T》与ObservableCollection《T》的用法基本上是一样的。区别:list《T》:当T继承于INotifyPropertyChanged时,如果list《T》中的属性发生改变,则通知UI属性值已发生改变。但当list《T》添加一项时,list《T》就无法通知前端UI了(此时,ObservableCollection《T》闪亮登场)。ObservableCollection《
WPF调用线程复制文件并显示进度条

WPF调用线程复制文件并显示进度条

在WPF中复制文件时,如何调用线程,显示进度条。《Window x:Class="WpfThreadTest.MainWindow"xmlns="http:schemas.microsoft.comwinfx2006xamlpresentation"xmlns:x="http:schemas.microsoft.comwinfx2006xaml"Title="MainWindow" Closed=
WPF中对XML的读写

WPF中对XML的读写

已知有一个XML文件(bookste.xml)如下:《?xmlversion="1.0" encoding="gb2312"?》《bookste》《bookgenre="fantasy" ISBN="236314"》《title》OberonsLegacy《title》《auth》Cets,Eva《auth》《price》5.95《price》《book》《bookste》 1、往《bookste

WPF使用word模板导出word文档

日期:2018-08-17 点击:1566 来源:PB2.CN

1、首先需要在word模板中插入书签
2、引用  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("标题", "标题");
                DList.Add("日期", DateTime.Now.ToString());
                DList.Add("内容", "内容");
                
                #endregion
                if (WordClass.ExportWord(urlstring, fileName, DList))
                {
                    MessageBox.Show("生成'" + fileName + "'成功!");
                }
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.ToString());
                return;
            }
        }
    }
}


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


这篇文档对您是否有帮助?

Winform 全屏显示

Winform 全屏显示

Winform程序无边框、置顶、全屏显示
WPF之Image控件

WPF之Image控件

Image控件加载图片包括加载动态图片,加载静态图片两种方式。加载动态图片通过生成一个BitmapImage,创建该对象后,赋给Image的Source即可。加载的形式:BitmapImage myBitmapImage =new BitmapImage();myBitmapImage.BeginInit();取得数据库存储的图片字段,MSSQL的Image类型Byte[] mybyte = Re
wpf list<T>与ObservableCollection<T>的区别

wpf list<T>与ObservableCollection<T>的区别

List《T》与ObservableCollection《T》的用法基本上是一样的。区别:list《T》:当T继承于INotifyPropertyChanged时,如果list《T》中的属性发生改变,则通知UI属性值已发生改变。但当list《T》添加一项时,list《T》就无法通知前端UI了(此时,ObservableCollection《T》闪亮登场)。ObservableCollection《
WPF调用线程复制文件并显示进度条

WPF调用线程复制文件并显示进度条

在WPF中复制文件时,如何调用线程,显示进度条。《Window x:Class="WpfThreadTest.MainWindow"xmlns="http:schemas.microsoft.comwinfx2006xamlpresentation"xmlns:x="http:schemas.microsoft.comwinfx2006xaml"Title="MainWindow" Closed=
WPF中对XML的读写

WPF中对XML的读写

已知有一个XML文件(bookste.xml)如下:《?xmlversion="1.0" encoding="gb2312"?》《bookste》《bookgenre="fantasy" ISBN="236314"》《title》OberonsLegacy《title》《auth》Cets,Eva《auth》《price》5.95《price》《book》《bookste》 1、往《bookste