C# 如何在asp.net中从excel读取数据并将其推入数组?
我是c#编程新手,想知道如何逐个单元格从excel中读取数据。在下面的代码中,我从excel的A列中获取一个数据数组,作为pValue1=A;BCd%;e%;f%;现在,如果a列的标题=ID,我只想将末尾带有%的值推送到不同的数组中。另外,我想用单引号将pValue1中的每个项括起来 输入: 身份证件 名称 A. 罗伊 B 也许 C 杰利 d% 导航 e% 十一月 f% 狮子座C# 如何在asp.net中从excel读取数据并将其推入数组?,c#,asp.net,arrays,excel,C#,Asp.net,Arrays,Excel,我是c#编程新手,想知道如何逐个单元格从excel中读取数据。在下面的代码中,我从excel的A列中获取一个数据数组,作为pValue1=A;BCd%;e%;f%;现在,如果a列的标题=ID,我只想将末尾带有%的值推送到不同的数组中。另外,我想用单引号将pValue1中的每个项括起来 输入: 身份证件 名称 A. 罗伊 B 也许 C 杰利 d% 导航 e% 十一月 f% 狮子座 不知道我是否理解你的问题。我想您已经将excel加载到了数据表中,现在只想将Id-列拆分为两个单独的列表。您可以使用L
不知道我是否理解你的问题。我想您已经将excel加载到了
数据表中
,现在只想将Id
-列拆分为两个单独的列表。您可以使用LINQ:
var percentLookup = excelData.AsEnumerable()
.ToLookup(row => row.Field<string>("Id").EndsWith("%"));
List<string> pValue1List = percentLookup[false]
.Select(row => row.Field<string>("Id"))
.ToList();
List<string> pValue2List = percentLookup[true]
.Select(row => row.Field<string>("Id"))
.ToList();
var percentLookup=excelData.AsEnumerable()
.ToLookup(row=>row.Field(“Id”).EndsWith(“%”);
List pValue1List=百分比查找[错误]
.Select(行=>row.Field(“Id”))
.ToList();
List pValue2List=百分比查找[真]
.Select(行=>row.Field(“Id”))
.ToList();
查找包含两个组,即id列末尾有百分比的行和其余的行。因此,您可以使用它轻松创建这两个列表
由于您是C#编程新手,因此最好使用普通循环:
List<string> pValue1List = new List<string>();
List<string> pValue2List = new List<string>();
foreach (DataRow row in excelData.Rows)
{
string id = row.Field<string>("Id");
if(id.EndsWith("%"))
{
pValue2List.Add(id);
}
else
{
pValue1List.Add(id);
}
}
List pValue1List=new List();
List pValue2List=新列表();
foreach(excelData.Rows中的DataRow行)
{
字符串id=行字段(“id”);
如果(id.EndsWith(“%”)
{
pValue2List.Add(id);
}
其他的
{
pValue1List.Add(id);
}
}
如果您需要字符串[]
而不是列表
请使用ToArray
而不是ToList
,在第二种方法中,填写列表,但在最后使用pValue1List.ToArray
一般来说:你应该停止使用ArrayList,因为ArrayList已经有20年的历史了,而且已经过时10多年了。使用强类型的
列表
,这里是列表
不确定我是否理解您的问题。我想您已经将excel加载到了数据表中
,现在只想将Id
-列拆分为两个单独的列表。您可以使用LINQ:
var percentLookup = excelData.AsEnumerable()
.ToLookup(row => row.Field<string>("Id").EndsWith("%"));
List<string> pValue1List = percentLookup[false]
.Select(row => row.Field<string>("Id"))
.ToList();
List<string> pValue2List = percentLookup[true]
.Select(row => row.Field<string>("Id"))
.ToList();
var percentLookup=excelData.AsEnumerable()
.ToLookup(row=>row.Field(“Id”).EndsWith(“%”);
List pValue1List=百分比查找[错误]
.Select(行=>row.Field(“Id”))
.ToList();
List pValue2List=百分比查找[真]
.Select(行=>row.Field(“Id”))
.ToList();
查找包含两个组,即id列末尾有百分比的行和其余的行。因此,您可以使用它轻松创建这两个列表
由于您是C#编程新手,因此最好使用普通循环:
List<string> pValue1List = new List<string>();
List<string> pValue2List = new List<string>();
foreach (DataRow row in excelData.Rows)
{
string id = row.Field<string>("Id");
if(id.EndsWith("%"))
{
pValue2List.Add(id);
}
else
{
pValue1List.Add(id);
}
}
List pValue1List=new List();
List pValue2List=新列表();
foreach(excelData.Rows中的DataRow行)
{
字符串id=行字段(“id”);
如果(id.EndsWith(“%”)
{
pValue2List.Add(id);
}
其他的
{
pValue1List.Add(id);
}
}
如果您需要字符串[]
而不是列表
请使用ToArray
而不是ToList
,在第二种方法中,填写列表,但在最后使用pValue1List.ToArray
一般来说:你应该停止使用ArrayList,因为ArrayList已经有20年的历史了,而且已经过时10多年了。使用强类型的
列表
,这里是列表
,非常感谢!这就像预期的一样…非常感谢!这是意料之中的事。。