C#讀取Word中的表格

      其實,MicroSoft提供了很多類和藉口供程序員們使用。最近一個項目中,需要對Word中的表格進行操作:因為老闆要求將內部控管系統做成Web版的,以便更好地跟臺灣溝通,但是網絡很慢,所以在網頁上輸入一些資料就很討厭,SO,需要制訂一個規格的文檔,比如Word,填寫好文檔後上傳到FTP上,我們再用一支程式掃描這些文檔,自動添加到數據庫中。。。一開始的時候,覺得好沒方向啊,到網上搜了一下,發現原來有很多前輩早在N年前就已經開始這方面的工作了。

1. 開始之前,請先加入參考MicroSoft Word 11.0 Object Library

    "參考"右鍵  ------>  加入參考  -------->  "加入參考"對話框選擇 "COM" 頁  -------->  在列表中找到MicroSoft Word 11.0 Object Library 雙擊選取後確定,會發現參考中多出來兩項:VBIDEWord/p>

2. 請添加using:

   using Word;

3. 讀取Word中的表格

  private void btnIn_Click(object sender, System.EventArgs e)
  {
   try
   {
    object fileName = "E://BB.doc";
    object readOnly = false;
    object isVisible = true;
    object missing = System.Reflection.Missing.Value;

    Word.ApplicationClass oWordApp = new Word.ApplicationClass();   //開啟應用程序WINWORD.EXE
      //打開要操作的Word文檔
    Word.Document oWordDoc = oWordApp.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);

 

    oWordDoc.Activate();

    //int i = oWordDoc.Tables.Count;   //文檔中表格的個數

    Word.Table nowTable = oWordDoc.Tables[1];   //文檔中第一個表格

    //讀取表格內容
    string strProjName = nowTable.Cell(2, 2).Range.Text.ToString();
    string strAssignEmployeeName = nowTable.Cell(3, 2).Range.Text.ToString();
    string strAssignDate = nowTable.Cell(3, 4).Range.Text.ToString();
    string strPreFinishDate = nowTable.Cell(4, 2).Range.Text.ToString();
    string strChargeEmployeeName = nowTable.Cell(5, 2).Range.Text.ToString();

    string strType = nowTable.Cell(8, 1).Range.Text.ToString();
    string strDesc = nowTable.Cell(8, 2).Range.Text.ToString();
    string strRem = nowTable.Cell(8, 3).Range.Text.ToString();

    //填充DataSet
    /**********************************************************************************************************
     //上面已經將表格中的內容讀出來了,當然可以將它們填到一個DataSet中了。。略過。。*_*
     **********************************************************************************************************/
    
    //關閉文檔
    oWordDoc.Close(ref missing, ref missing, ref missing);   //關閉文檔
    oWordApp.Quit(ref missing, ref missing, ref missing);     //關閉應用程序WINWORD.EXE

   }  
   catch(Exception Ex)
   {
    comGCtlFun.gShowNotifyMsg(this.Page,"失敗!//n錯誤信息:"+Ex.Message ,"操作提示","454","367" );
   }
  }

4.  將數據庫中的資料輸出到Word中,以表格形式保存

  private void btnWD_Click(object sender, System.EventArgs e)
  {
   try  
   {
    string sql = "select * from BookStore";
    DataSet storedt = GetDataSet(sql);
    
    Object Nothing = System.Reflection.Missing.Value;
    object filename = @"E:/AA.doc";     //要保存到E:/AA.doc
    Word.Application WordApp = new Word.ApplicationClass();
    Word.Document WordDoc = WordApp.Documents.Add(ref Nothing,ref Nothing,ref Nothing,ref Nothing);
   
    WordDoc.Paragraphs.First.Range.Text = "庫存報表[共有:" + storedt.Tables[0].Rows.Count.ToString() + "本書]";
    WordDoc.Paragraphs.First.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;  

    Word.Table table=WordDoc.Tables.Add(WordApp.Selection.Range,storedt.Tables[0].Rows.Count+1, 6, ref Nothing, ref Nothing);  
    table.Cell(1,1).Range.Text="ISBN號";
    table.Cell(1,2).Range.Text="書名";
    table.Cell(1,3).Range.Text="總庫存";
    table.Cell(1,4).Range.Text="借書庫存";
    table.Cell(1,5).Range.Text="可借數量";
    table.Cell(1,6).Range.Text="已借數量";
   
    for(int i = 0; i < storedt.Tables[0].Rows.Count; i++)
    {
     table.Cell(i+2,1).Range.Text = storedt.Tables[0].Rows[i]["Book_ISBN"].ToString();
     table.Cell(i+2,2).Range.Text = storedt.Tables[0].Rows[i]["Book_Name"].ToString();
     table.Cell(i+2,3).Range.Text = storedt.Tables[0].Rows[i]["Store_Num"].ToString();
     table.Cell(i+2,4).Range.Text = storedt.Tables[0].Rows[i]["CanBorrow_Num"].ToString();
     table.Cell(i+2,5).Range.Text = storedt.Tables[0].Rows[i]["InShop_Num"].ToString();
     table.Cell(i+2,6).Range.Text = storedt.Tables[0].Rows[i]["OutShop_Num"].ToString();
    }
   
    WordDoc.SaveAs(ref filename, 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.Close(ref Nothing, ref Nothing, ref Nothing);   //關閉Word文檔
    WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);      //退出應用程序WINWORD.EXE

    comGCtlFun.gShowNotifyMsg(this.Page,"庫存報表導出成功!","操作提示","454","367" );
   }  
   catch(Exception Ex)
   {
    comGCtlFun.gShowNotifyMsg(this.Page,"庫存報表導出失敗!//n錯誤信息:"+Ex.Message ,"操作提示","454","367" );
   }
  }

private DataSet GetDataSet(string sql)
  {
   constring = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
   SqlDataAdapter sda = new SqlDataAdapter(sql,constring);
   DataSet ds = new DataSet();
   sda.Fill(ds);
   return ds;
  }

5. 好了,大功告成以上代碼經測試成功。<操作系統:WIN2003OFFICE2003>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章