基本信息
源码名称:c#根据word模板生成word文档
源码大小:0.04M
文件格式:.rar
开发语言:C#
更新时间:2016-03-09
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

c#根据word模板生成word文档,需要本机安装Microsoft Office


  

using System;
using System.Collections.Generic;
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;

//需要引入的DLL
using System.IO;
using Microsoft.Office.Core;//COM选项卡中的"Microsoft Office 11.0 Object Library"
using Word = Microsoft.Office.Interop.Word;//.NET选项卡中的"Microsoft.Office.Interop.Word"

//调用模板生成word(word模板中需要添加书签项以便程序生成数据,如果word模板中没有书签则会发生错误)
namespace WpfAppExportWord
{
    /// <summary>
    /// WindowExportWord.xaml 的交互逻辑
    /// </summary>
    public partial class WindowExportWord : Window
    {
        public WindowExportWord()
        {
            InitializeComponent();
        }

        private void Btn1_Click(object sender, RoutedEventArgs e)
        {
            //模板文件
            string templateFile =  @"D:\Word模板.doc";
            //生成的具有模板样式的新文件
            string fileName = @"D:\导出后的Word文件"   DateTime.Now.ToString("yyyyMMddHHmmss")   ".doc";

            ExportWord(templateFile, fileName);
        }

        /// <summary>
        /// 调用模板生成word
        /// </summary>
        /// <param name="templateFile">模板文件</param>
        /// <param name="fileName">生成的具有模板样式的新文件</param>
        public void ExportWord(string templateFile, string fileName)
        {
            try
            {
                //生成word程序对象
                Word.Application app = new Word.Application();

                //模板文件
                string TemplateFile = templateFile;
                //生成的具有模板样式的新文件
                string FileName = fileName;

                //模板文件拷贝到新文件
                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();

                int WordNum = 4;//书签个数
                //将光标转到模板中定义的书签的位置,插入所需要添加的内容,循环次数与书签个数相符
                for (int WordIndex = 1; WordIndex <= WordNum; WordIndex  )
                {
                    object WordMarkName = "书签名称"   WordIndex.ToString();//word模板中的书签名称
                    object what = Word.WdGoToItem.wdGoToBookmark;
                    doc.ActiveWindow.Selection.GoTo(ref what, ref missing, ref missing, ref WordMarkName);//光标转到书签的位置
                    doc.ActiveWindow.Selection.TypeText("插入的内容"   WordIndex.ToString());//插入的内容,插入位置是word模板中书签定位的位置
                    doc.ActiveWindow.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;//设置当前定位书签位置插入内容的格式
                    //doc.ActiveWindow.Selection.TypeParagraph();//回车换行
                }

                //输出完毕后关闭doc对象
                object IsSave = true;
                doc.Close(ref IsSave, ref missing, ref missing);

                MessageBox.Show("生成“"   FileName   "”成功!");
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.ToString());
                return;
            }
        }
    }
}