C#用模版生成word文档

1、 环境

Windows 2003 server

Office 2003

Vs2005

.net framework 2.0

2、 步骤

1、   新建模版保存到E:/word/1.dot;

2、   在新建的模版中添加入书签demo到指定的位置;

3、   新建windows 应用程序demo;

4、   在程序中添加引用Microsoft Word 11.0 Object Library;

5、   编写程序;

3、 代码

using Word = Microsoft.Office.Interop.Word;

using System.Reflection;

加入上面两条using语句

添加一个textbox名为:demo

添加一个按钮,在其事件中写入:

    object oMissing = System.Reflection.Missing.Value;

    //Start Word and create a new document.

    Word._Application oWord;

    Word._Document oDoc;

    object oTemplate = "E://word//1.dot";

    oWord = new Word.Application();

    oWord.Visible = false;

    oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing,

                ref oMissing, ref oMissing);

    object oBookMark = "demo";

    oDoc.Bookmarks.get_Item(ref oBookMark).Range.Text = this.demo.Text.Trim();

    object oFilename = "E://demo//1.doc";

    oDoc.SaveAs(ref oFilename, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

   oDoc.Close(ref oMissing,ref oMissing,ref oMissing);

   oWord.Quit(ref oMissing, ref oMissing, ref oMissing);

this.Close();

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值