zoukankan      html  css  js  c++  java
  • C#2.0 读word的多个表格到DataGridView或是其它控件 XP Vista

     1 #region DataTalbe 要考虑第一行是表头,第二行开始为数据
     2 
     3             try
     4             {
     5                 //tablePos表格序号,就是查找word文档几个表格的自动给的序号
     6                 for (int tablePos = 1; tablePos <= oDoc.Tables.Count; tablePos++)
     7                 {
     8 
     9                     string t = tablePos.ToString();
    10                     DataSet ds = new DataSet();
    11                     DataTable dt = new DataTable(t);
    12                     // DataGridView dgv = new DataGridView();
    13                     ds.Tables.Add(dt);
    14 
    15                     Word.Table nowTable = oDoc.Tables.Item(tablePos);
    16                     DataRow dr;
    17 
    18                     //取得第一行标题值
    19                     for (int rowPos = 1; rowPos <= nowTable.Rows.Count; rowPos++)
    20                     {
    21                         //第一行值                               
    22                         if (rowPos == 1)
    23                         {
    24                             for (int columPos = 1; columPos <= nowTable.Columns.Count; columPos++)
    25                             {
    26                                 string tableMessage = nowTable.Cell(rowPos, columPos).Range.Text;
    27                                 tableMessage = tableMessage.Remove(tableMessage.Length - 22).Trim(); ;
    28                                 //只求到了第一行值  
    29                                 dt.Columns.Add(tableMessage);
    30 
    31 
    32 
    33                             }
    34                         }
    35                         else
    36                         {
    37                             //第二行值
    38                             dr = dt.NewRow();
    39                             for (int columPos = 1; columPos <= nowTable.Columns.Count; columPos++)
    40                             {
    41                                 string tableMessage = nowTable.Cell(rowPos, columPos).Range.Text;
    42                                 tableMessage = tableMessage.Remove(tableMessage.Length - 22).Trim();
    43                                 dr[columPos - 1= tableMessage;
    44 
    45                             }
    46                             dt.Rows.Add(dr);
    47 
    48                         }
    49                     }
    50 
    51                     dataGridView1.DataSource = ds.Tables[t].DefaultView;
    52                    
    53                 }
    54 
    55 
    56             }
    57             catch (Exception ex)
    58             {
    59                 //throw;
    60                 MessageBox.Show("你选择的文档不是表格格式不对""提示");
    61             }
    62             oReadOnly = true;
    63             Cursor = Cursors.Default;
    64             #endregion
  • 相关阅读:
    [ZT]数据表:USR02(登录数据)
    ASP.NET Web 应用程序与ASP.NET网站比较
    DataTable排序的一般方法
    如何重建開啟被锁的SAP超级用戶帐号
    SQL語句中时间格式的转换
    [ZT]提高 Ajax 应用程序性能,避开 Web 服务漏洞
    SAP NetWeaver
    SAP安装步骤[ECC6]WIN2000AS
    SAP&SAP Solution Manager中的常用命令
    [转]如何在Visio 2007中画接口和实现类的关系图
  • 原文地址:https://www.cnblogs.com/geovindu/p/1417137.html
Copyright © 2011-2022 走看看