利用.net替换Word的内容(从数据库中取数据来替换word里面的书签)(转)

 

hong

转自:http://blog.csdn.net/lshzfq/

 

 
整个过程是这样的

1.
    首先需要将word的dll引入进来,如果装了word的话,会在他的安装目录下面有一个MSWORD9.OLB文件(通过添加引用即可)这里需要注意的是上面这个文件,可能会因为office的版本不一样,文件名有所不同,而且在下面的open和save方法的参数也会因为版本的不同而不同,office2003中的open方面的参数好象是16个,而2000里的参数大概只有12个,调用的时候一定要注意
2.
要在webconfig文件里面加上一句:   <identity impersonate="true"/> 主要是模拟身份的吧,如果不加的话,程序运行的时候会报出拒绝访问的错误的.(而且你需要预先做好一个带书签的word模板)
3.
    新建立一个也面,在面上部加如using Word;
    下面就是具体的函数了:(我这里的函数没有整理过,可能有些没用)
打开文件:
 private Word.Document OpenDoc(string strDocPath,ref Word.Application WordApp,int flag)
  {
   if (!File.Exists(strDocPath))
    return null;
   object fileName = (object)strDocPath;   
   object isVisible = missing;
   object readOnly = missing;
   //Make word visible, so you can see what's happening
   WordApp.Visible = false;
   //通过open创建一个Word.Document的实例
   Word.Document doc = null;
   try
   {
    //doc = WordApp.Documents.Open(ref fileName, ref missing,ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible,ref missing,ref missing,ref missing,ref missing);
    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);
    //if (flag == 1)
     //ListStyle(doc);
    return doc;
   }
   catch(Exception Ex)
   {
    throw new Exception(Ex.Message);
    return null;
   } 
  }

替换模板内容:
string strWordTemplate = Server.MapPath("../test/办文单.doc"); //这里是你的模板文件
   Word.Application WordApp = new Word.ApplicationClass();  //  定义一个Word.Application 对象
   Word.Document doc = OpenDoc(strWordTemplate,ref WordApp,1); //定义一个Word.Document 对象
   try
   {
//下面是从数据库取数据,不好意思这个代码有点烂
    DataTable TempTable=this.CreateTable("Select * from Workflow_BW where AppID="+Convert.ToInt32(AppID)+" and ContentID="+Convert.ToInt32(ContentID));  
    if(TempTable.Rows.Count>0)
    {
     string TempTime=TempTable.Rows[0]["SWTime"].ToString().Trim();
     int Pos=TempTime.IndexOf(" ");
     string All=TempTime.Substring(0,Pos);
     int Pre=All.IndexOf("-");
     int Next=All.LastIndexOf("-");
     string Year=All.Substring(0,Pre).Trim();
     string Month=All.Substring(Pre+1,Next-Pre-1).Trim();
     string Day=All.Substring(Next+1,All.Length-Next-1).Trim();
     foreach(Word.Bookmark BM in doc.Bookmarks)  //这是最关键的地方:对文档的所有书签进行便利匹配
     {
      switch(BM.Name)
      {
       case "Advice": //替换Advice书签的内容,其他一样
        BM.Select();
        BM.Range.Text=this.CreateTable("Select Advice from Workflow_Advice where AppID="+Convert.ToInt32(AppID)+" and ContentID="+Convert.ToInt32(this.ContentID)+" and StepID=1").Rows[0]["Advice"].ToString().Trim();
        break;
       case "Day":
        BM.Select();
        BM.Range.Text=Day;
        break;
       case "LWDW":
        BM.Select();
        BM.Range.Text=TempTable.Rows[0]["LWDW"].ToString().Trim();
        break;
       case "LWH":
        BM.Select();
        BM.Range.Text=TempTable.Rows[0]["SWH"].ToString().Trim();
        break;
       case "Month":
        BM.Select();
        BM.Range.Text= Month;
        break;
       case "NowYear":
        BM.Select();
        BM.Range.Text=Year;
        break;
       case "Subject":
        BM.Select();
        BM.Range.Text=TempTable.Rows[0]["Subject"].ToString().Trim();
        break;
       case "SWH":
        BM.Select();
        BM.Range.Text=TempTable.Rows[0]["LSH"].ToString().Trim().Substring(4,TempTable.Rows[0]["LSH"].ToString().Trim().Length-4);
        break;
      }         
     }
    }
    object fn = (object)Server.MapPath("../test/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里的打印功能
    //doc.SaveAs(ref fn, ref missing,ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,ref missing, ref missing, ref missing,ref missing,ref missing,ref missing,ref missing);    
   }
   catch(Exception err)
   {
    this.tbXml.Text = err.ToString();
   }
   finally
   {
    doc.Close(ref missing,ref missing,ref missing); 
    WordApp.Quit(ref missing,ref missing,ref missing);
    WordApp = null; 
   }
  }

这里面一个最主要的问题, doc.Close(ref missing,ref missing,ref missing); 
    WordApp.Quit(ref missing,ref missing,ref missing);
    WordApp = null; 
这个代码好象不起作用,每次关闭打印的时候都会抱word错误,而且进程里面winword.exe也没关,不知道怎么回事.
关于word里面的一些对象如:Application document Selection Range BookMark对象,本人也不是很熟悉,请参考MSDN上的帮助,那里面讲的很详细.希望对大家有所帮助


 
posted @ 2009-05-21 15:52  Candyxiaoqiang  阅读(293)  评论(0编辑  收藏  举报