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>

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