C#操作Word文档

1.添加引用Microsoft.Office.Interop.Word.dll。若找不到则添加MIscrosoft word 11.0 Object Library 引用(在com组件中,具体的版本可能不同)

一、C#打开一个word文档

页面添加Word引用:using Microsoft.Office.Interop.Word.

Microsoft.Office.Interop.Word.Application myWordApp = new   Microsoft.Office.Interop.Word.ApplicationClass();
Microsoft.Office.Interop.Word.Document myWordDoc=new Document ();    //以下将主要用到此处所建立的对象

   object filepath = @"F://test2//wordtest.doc"; //将要打开的Word文档路径

   if (!System.IO.File.Exists((string)strFileName))

{

Response.Write("<script language='javascript'>alert('没有找到该目录下的文件!');</script>");

return ;

}
   object oMissing = System.Reflection.Missing.Value;
   myWordDoc = myWordApp.Documents.Open(ref filepath, 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);
   myWordDoc.Content.Font.Size = 24.0f;//可省略
   myWordDoc.Content.Font.Name = "Arial Unicode MS";//可省略
   myWordDoc.Save();
   richTextBox1.Text = myWordDoc.Content.Text;
   myWordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
   myWordApp.Quit(ref oMissing, ref oMissing, ref oMissing);

二、创建一个Word文档

   strFileName = @"F://test2//wordtest.doc"; // 将要创建的Word文档
   if (System.IO.File.Exists((string)strFileName))
   System.IO.File.Delete((string)strFileName);
   Object Nothing = System.Reflection.Missing.Value;
   myWordDoc = myWordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
  
   #region 将数据库中读取得数据写入到word文件中
  
   strContent = "你好/n/n/r";
   myWordDoc.Paragraphs.Last.Range.Text = strContent;
  
   strContent = "这是测试程序";
   myWordDoc.Paragraphs.Last.Range.Text = strContent;
  
  
   #endregion
  
   //将WordDoc文档对象的内容保存为DOC文档
   myWordDoc.SaveAs(ref strFileName, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
   //关闭WordDoc文档对象
   myWordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
   //关闭WordApp组件对象
   myWordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
  
   this.richTextBox1.Text = strFileName + "/r/n" + "创建成功";

三、替换Word文档的内容

         1.在Word模板中添加标签"ashen331"(可以添加n个),下面例子以一个标签"ashen331"为例.

            object omissing = System.Reflection.Missing.Value;
            object isreadonly = false;
            object template = @"F://test2//wordtest.doc";//将要打开的Word文档
            object savetoword = @"F://test2//saveword.doc";//作变动后,要保存到的目标word文档(相当另存为)
        try
        {
            // open
            myWordDoc = myWordApp.Documents.Open(ref template, ref omissing, ref isreadonly, 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);
          
            // change

                      object name = "ashen331";
            if (myWordApp.ActiveDocument.Bookmarks.Exists(name.ToString()))
            {
                myWordDoc.Bookmarks.get_Item(ref name).Range.Text = "我的博客";//将覆盖ashen331标签,如果选择文字插入标签的,则原文字将被覆盖,修改过后的文件将不存在此标签。如果没有选择文字插入标签 的,则将在标签位置插入文字,修改过后的文件依然存在此标签。
                myWordDoc.Bookmarks.get_Item(ref   name).Select();
            }

          //safe
            myWordDoc.SaveAs(ref savetoword, 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);
             }
        catch (Exception ex)
        {
            throw new Exception("Write word document failed", ex);
        }
        finally
        {
            if (myWordDoc != null)
            {
               // Object saveChanges = myWordApp.Options.BackgroundSave;
                myWordDoc.Close(ref omissing, ref omissing, ref omissing);
            }
            if (myWordApp != null)
            {
                myWordApp.Quit(ref omissing, ref omissing, ref omissing);
            }

        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值