gpt4 book ai didi

c# - 使用 linqtoexcel 从 excel 文件中获取所有值

转载 作者:太空宇宙 更新时间:2023-11-03 19:59:03 25 4
gpt4 key购买 nike

我在我的 asp.net mvc 4 项目中使用 linqtoexcel 来读取 excel 文件并从那里获取所有值。但我只得到最后一行的值。这是我的代码,

Controller

    public ActionResult ExcelRead()
{
string pathToExcelFile = ""
+ @"C:\MyFolder\ProjectFolder\sample.xlsx";

string sheetName = "Sheet1";

var excelFile = new ExcelQueryFactory(pathToExcelFile);
var getData = from a in excelFile.Worksheet(sheetName) select a;

foreach (var a in getData)
{
string getInfo = "Name: "+ a["Name"] +"; Amount: "+ a["Amount"] +">> ";
ViewBag.excelRead = getInfo;
}
return View();
}

查看

@ViewBag.excelRead

如何从所有行中获取值?非常需要这个帮助!谢谢。

最佳答案

试试这个(扩展@Sachu 对问题的评论)-

public ActionResult ExcelRead()
{
string pathToExcelFile = ""
+ @"C:\MyFolder\ProjectFolder\sample.xlsx";

string sheetName = "Sheet1";

var excelFile = new ExcelQueryFactory(pathToExcelFile);
var getData = from a in excelFile.Worksheet(sheetName) select a;
string getInfo = String.Empty;

foreach (var a in getData)
{
getInfo += "Name: "+ a["Name"] +"; Amount: "+ a["Amount"] +">> ";

}
ViewBag.excelRead = getInfo;
return View();
}

关于c# - 使用 linqtoexcel 从 excel 文件中获取所有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30432006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com