0

我有个问题。我有 2 个数据表,想将第一个表的 3 列复制到另一个表的 3 列。我怎样才能跳过 3 第一行然后将值添加到 2.table?

我的代码如下所示:

 foreach (DataRow ex1 in excelTb1.Rows)
            {
                foreach (DataRow ex2 in excelTb2.Rows)
                {                      

                    //ex2["ABC"] = ex1["ABC"];      // with skip(3) ?
                    //ex2["Name"] = ex1["Name"];
                    //ex2["ID"] = ex1["ID"];
                }
            }

我的 1.table:_ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ _ _我的 2.table 应该看起来像这样:

          Table1                               Table2
ABC   Name   ID  ...                     ABC   Name   ID    ...                     
 a     lola   2  ...                     ...   ...    ...   ... 
 b     kiki   6  ...                     ...   ...    ...   ... 
...    ...   ... ...                     ...   ...    ...   ... 
                                         a     lola   2     ...
                                         b     kiki   6     ...        
                                         ...   ...    ...   ...             
4

2 回答 2

2

跳过前 3 行

使用 For 循环而不是 foreach。

for(int i=3; i < excelTb2.Rows.Count; i++) //start the loop with index 3 => Row 4
{
    DataRow ex2 = exceltb2.Rows[i];
    //ex2["ABC"] = ex1["ABC"];      // with skip(3) ?
    //ex2["Name"] = ex1["Name"];
    //ex2["ID"] = ex1["ID"];
}
于 2012-11-15T09:40:14.767 回答
1
for (int i = 0; i < excelTb1.Rows.Count; i++)
{
     DataRow dr1 = excelTb1.Rows[i];

     if (excelTb2.Rows.Count > i + 3)
     {
         DataRow dr2 = excelTb2.Rows[i + 3];
         dr2["ABC"] = dr1["ABC"];
         dr2["Name"] = dr1["Name"];
         dr2["ID"] = dr1["ID"];
     }
     else
         break;
}
于 2012-11-15T09:46:28.387 回答