axWebBrowser调用Word控件,Word控件标签内插入数据。修改后保存。

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;

namespace TEst2
{
 /// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private object missing=System.Reflection.Missing.Value;
  private AxSHDocVw.AxWebBrowser axWebBrowser1;
  private System.Windows.Forms.Button button1;
  private System.ComponentModel.IContainer components;

  public Form1()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
   this.axWebBrowser1 = new AxSHDocVw.AxWebBrowser();
   this.button1 = new System.Windows.Forms.Button();
   ((System.ComponentModel.ISupportInitialize)(this.axWebBrowser1)).BeginInit();
   this.SuspendLayout();
   //
   // axWebBrowser1
   //
   this.axWebBrowser1.Enabled = true;
   this.axWebBrowser1.Location = new System.Drawing.Point(16, 32);
   this.axWebBrowser1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axWebBrowser1.OcxState")));
   this.axWebBrowser1.Size = new System.Drawing.Size(688, 408);
   this.axWebBrowser1.TabIndex = 0;
   //
   // button1
   //
   this.button1.Location = new System.Drawing.Point(528, 472);
   this.button1.Name = "button1";
   this.button1.Size = new System.Drawing.Size(96, 32);
   this.button1.TabIndex = 1;
   this.button1.Text = "button1";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(728, 533);
   this.Controls.Add(this.button1);
   this.Controls.Add(this.axWebBrowser1);
   this.Name = "Form1";
   this.Text = "Form1";
   this.Load += new System.EventHandler(this.Form1_Load);
   ((System.ComponentModel.ISupportInitialize)(this.axWebBrowser1)).EndInit();
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }
  private void Form1_Load(object sender, System.EventArgs e)
  {
  
   ShowBorrow();
   axWebBrowser1.Navigate(System.AppDomain.CurrentDomain.BaseDirectory+"temp.doc");
  }
  public void ShowBorrow()
  {
   //替换模板内容:
   string strWordTemplate =System.AppDomain.CurrentDomain.BaseDirectory+"NoteAsk.dot";//这里是你的模板文件
   Word.Application WordApp = new Word.ApplicationClass();   定义一个Word.Application 对象
   Word.Document doc = OpenDoc(strWordTemplate,ref WordApp,1); 定义一个Word.Document 对象
   try
   {
    foreach(Word.Bookmark BM in doc.Bookmarks) 这是最关键的地方:对文档的所有书签进行便利匹配
    {
     switch(BM.Name.ToLower())
     {
      case "age": 替换Advice书签的内容,其他一样
       BM.Select();
       BM.Range.Text="ha ha ...";
       break;
      case "address":
       BM.Select();
       BM.Range.Text="address";
       break;
     }        
    }
    object fn = (object)System.AppDomain.CurrentDomain.BaseDirectory+"temp.doc";   
    doc.SaveAs(ref fn, ref missing,ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,ref missing, ref missing);
    /// 这里是另存为一个一个文件
   }                                        // Response.Redirect("../test/temp.doc");//直接打开用ie打开另存的文件,然后可直接调用ie里的打印功能
   catch(Exception Ex)
   {
    throw new Exception(Ex.Message);
   }
   finally
   {
    doc.Close(ref missing,ref missing,ref missing); 
    WordApp.Quit(ref missing,ref missing,ref missing);
    WordApp = null;
    
   }
  }
  private Word.Document OpenDoc(string strDocPath,ref Word.Application WordApp,int flag)
  {
   if (!System.IO.File.Exists(strDocPath))
    return null;
   object fileName = (object)strDocPath;  
   object isVisible = missing;
   object readOnly = missing;
   WordApp.Visible = false;
   //通过open创建一个Word.Document的实例
   Word.Document doc = null;
   try
   {
    doc = WordApp.Documents.Open(ref fileName, ref missing,ref missing,ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,ref missing, ref missing, ref missing);
    return doc;
   }
   catch(Exception Ex)
   {
    throw new Exception(Ex.Message);
   }
  }
  //点确定后保存修改
  private void button1_Click(object sender, System.EventArgs e)
  {
    axWebBrowser1.ExecWB(SHDocVw.OLECMDID.OLECMDID_SAVE  , SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER  );
  }

 }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值