读取excel表时数据集问题 [英] Dataset problem while reading excel sheet

查看:58
本文介绍了读取excel表时数据集问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在从Excel工作表中读取数据并使用OLEDB命令用excel数据填充适配器。



我使用以下代码检查第3列消息名称是否以GW开头。



工作正常。但是我在代码中使用Rows来检查excel中的列





 foreach(lstDS中的DataRow行[0] .Tables [0] .Rows)LT峰; br /> 
{<峰; br />
将峰; br />
串fieldFromDB =行[2]的ToString() ;< br />
if(fieldFromDB.StartsWith(GW))





我很困惑,是数据集以相反的顺序存储excel的值(比如列和行的交换)



现在我需要做类似的事情,如果第3个消息名称列以GW开头,然后我需要在同一行的第二列中复制相应的消息ID



任何人都可以解释一下这是怎么回事完成。





谢谢

John

解决方案

我在你的代码中添加了一些注释并添加了几行。也许它会有所帮助。如果我正确理解你的意图,你想要检查表的每一行,如果它以GW字符串开头,则将第三列的内容复制到第二列,对吗?





的foreach(DataRow的行中lst​​DS [0] .Tables [0] .Rows)
{

串fieldFromDB =行[2]的ToString();

//以下行检查第三列的内容是否以GW开头
if(fieldFromDB.StartsWith(GW))
{
//如果上述条件为'true',则将第三列的值赋给第二列
row [1] = row [2];
}
}



并回答你问题的另一部分。 DataSet不会切换带有行的列。



如果您的Excel工作表看起来很糟糕,您最终会得到包含3列和2行的DataTable。



柱#1&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;柱#2&NBSP;&NBSP;柱#3

ABC&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; DEF&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; GH

AXX&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; CC&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP; GWA


Hi,

I am reading the data from excel sheet and using OLEDB command to fill the adapter with excel data.

I am using the following code to check whether 3rd column message names are starting with GW or not.

It is working fine. But here I am using Rows in the code for checking columns in excel


foreach (DataRow row in lstDS[0].Tables[0].Rows)<br />
           {<br />
<br />
               string fieldFromDB = row[2].ToString();<br />
               if (fieldFromDB.StartsWith("GW"))



I am confused, Is the dataset stores the values of excel in reverse order(like interchanging of columns and rows)

Now I need to do something like, if the message name in 3rd column starts with GW, then I need to copy the corresponding message ID in 2nd column of the same row

Can anyone please explain me how this can be done.


Thanks
John

解决方案

I added some comments to your code and added few lines. Perhaps it will help. If I correctly understand your intention you want to examine every row of the table and copy contents of the third column to the second column if it begins with "GW" string, correct?


foreach (DataRow row in lstDS[0].Tables[0].Rows)
{

string fieldFromDB = row[2].ToString();

//The following line is checking if content of the third column starts with "GW"
if (fieldFromDB.StartsWith("GW"))
{
   //If above condition is 'true', assign value of third column to the second column 
   row[1]=row[2];
}
}


And to answer another part of your question. DataSet doesn’t switch columns with rows.

If your Excel sheet looks like blow you will end up with DataTable containing 3 columns and 2 rows.

Column#1     Column#2  Column #3
abc              def             gh
axx              cc              GWa


这篇关于读取excel表时数据集问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆