1. 程式人生 > >利用.net替換Word的內容(從資料庫中取資料來替換word裡面的書籤)

利用.net替換Word的內容(從資料庫中取資料來替換word裡面的書籤)

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上的幫助,那裡面講的很詳細.希望對大家有所幫助