c# winform word模板 利用WORD 书签 定位替换 生成 WORD

15 篇文章 0 订阅
 private void button1_Click(object sender, EventArgs e)
        {
            //模板文件
            string templateFile = @"Word模板.doc";
            //生成的具有模板样式的新文件
            string fileName = @"F:\WinMain\bin\Debug\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".doc";

            ExportWord(templateFile, fileName);
        }

        #region MyRegion
         /// <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);
                app.Quit(ref missing, ref missing, ref missing);

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

//需要引入的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 模板:

待插入内容标题1

 

待插入内容标题2

 

待插入内容标题3

 

待插入内容标题4

 

word 书签插入方法:

设置书签方法/步骤

 

  1. 将光标定位到要设置书签的地方,或选中要设置书签的文字,点击“插入”菜单 下的“书签”。

    word书签怎么添加、使用、显示和删除

  2. 2

    输入书签名,点击“添加”即可。注意,书签名必须以字母或汉字开头,可以包含数字和下划线,但不能包含“@”、“#”、“~”等符号。

    word书签怎么添加、使用、显示和删除

    END

使用书签方法/步骤2

  1. 点击“插入”菜单下的“书签”,双击已有的书签可以直接跳转到书签处,或选中书签,点击“定位”,也可以跳转到书签处。

    word书签怎么添加、使用、显示和删除

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值