【发布时间】:2015-05-25 21:31:26
【问题描述】:
我在我的asp.net mvc 4 项目中使用linqtoexcel 来读取一个excel 文件并从那里获取所有值。但我只得到最后一行的值。这是我的代码,
控制器
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
如何从所有行中获取值?非常需要这个帮助!谢谢。
【问题讨论】:
-
在 foreach 循环中,Viewbag.excelRead 正在分配当前行..这就是为什么您只能在视图中看到最后一行..您应该将其分配给一个列表并在视图中调用该列表
-
谢谢。你能通过代码给我一个例子吗?我在 asp.net 中有点菜鸟。
标签: c# asp.net-mvc excel linq-to-excel