利用.net替换Word的内容(从数据库中取数据来替换word里面的书签)
        我在现在的项目中,用户需要打印办文单,一开始做的时候,就用是用表格,按照他们的格式划了一个,可是,打出来的东西总是和他们自己的不相同,(一句话,不好看),我也头疼一阶段没什么好的办法,后来想,干脆把他们word模板拿过来,自己来替换.刚开始的时候,不知道怎么做问了,google找了,但没有相同的满意的.后来还是在msdn上找了一点点,然后请教了同事.在总算搞定.效果还不错.

整个过程是这样的:(本人用的是asp.net+C#)
1.
     首先需要将word的dll引入进来,假如装了word的话,会在他的安装目录下面有一个MSWORD9.OLB文档(通过添加引用即可)这里需要注意的是上面这个文档,可能会因为office的版本不相同,文档名有所不同,而且在下面的open和save方法的参数也会因为版本的不同而不同,office2003中的open方面的参数好象是16个,而2000里的参数大概只有12个,调用的时候一定要注意
2.
要在webconfig文档里面加上一句:    主要是模拟身份的吧,假如不加的话,程式运行的时候会报出拒绝访问的错误的.(而且您需要预先做好一个带书签的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;
    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上的帮助,那里面讲的很周详.希望对大家有所帮助

博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3