[英]How can I iterate over an excel file
How can I iterate over an excel file? 如何遍历Excel文件?
I currently have a class using ExcelDataReader 我目前有一个使用ExcelDataReader的类
Class => https://paste.lamlam.io/nomehogupi.cs#14fXzlopygZ27adDcXEDtQHT0tWTxoYR 类别=> https://paste.lamlam.io/nomehogupi.cs#14fXzlopygZ27adDcXEDtQHT0tWTxoYR
I have a excel file with 5 columns 我有5列的Excel文件
This is my current code, but it is not exporting the result that I expect ... 这是我当前的代码,但是没有导出我期望的结果...
TextWriter stream = new StreamWriter("excel Path");
//foreach string in List<string>
foreach(var item in ComboList) {
var rows = ExcelHelper.CellValueCollection(item.Key);
foreach(var row in rows) {
stream.WriteLine(item.Key + "|" + row);
break;
}
}
stream.Close();
My result: 我的结果:
Column1|Row1
Column1|Row2
Column1|Row3
...
Column2|Row1
Column2|Row2
Column2|Row3
...
Expected: 预期:
Column1|Row1|Column2|Row1...
Column1|Row2|Column2|Row2...
Column1|Row3|Column2|Row3...
Thanks 谢谢
The first problem is that you are asking to write two items and only two items on a single line. 第一个问题是您要在一行上写两个项目,而只写两个项目。
Would it help if you made the stream.writeline() statement into a .write() statement and then, after the inner loop performed a .writeline() which would terminate the line? 如果将stream.writeline()语句转换为.write()语句,然后在内部循环执行.writeline()之后终止该行,是否有帮助?
Apologies for not commenting but not enough Respect points to do so. 很抱歉没有发表评论,但没有足够的尊重表示这样做。 - Malc
-马尔克
This is the answer, I just needed to get the dataSet and iterate over it, very easy 这就是答案,我只需要获取数据集并对其进行迭代,非常简单
var data = ExcelHelper.DataSet();
foreach (DataRow dr in data.Tables[0].Rows)
{
Console.WriteLine(dr["Column1"] + "|" + dr["Column2"]);
}
If I understand what you want truly! 如果我真正了解您想要什么! I think you need to add a method like
RowValueCollection
to your ExcelHelper
as below: 我认为您需要向您的
ExcelHelper
添加诸如RowValueCollection
类的方法,如下所示:
public static IEnumerable<string[]> RowValueCollection()
{
var result = Data.Tables[0].Rows.OfType<DataRow>()
.Select(dr => dr.ItemArray.Select(ia => ia.ToString()).ToArray());
return result;
}
And then use it like this: 然后像这样使用它:
var rowValues = ExcelHelper.RowValueCollection();
foreach (var row in rowValues)
{
stream.WriteLine(string.Join("|", row));
}
HTH ;) HTH;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.