使用 linqtoexcel 从 excel 文件中获取所有值
Get all the values from excel file by using linqtoexcel
我在我的 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
如何从所有行中获取值?非常需要这个帮助!谢谢。
将 getDate 设为 .ToList()
var getData = (from a in excelFile.Worksheet(sheetName) select a);
List<string> getInfo = new List<string>();
foreach (var a in getData)
{
getInfo.Add("Name: "+ a["Name"] +"; Amount: "+ a["Amount"] +">> ");
}
ViewBag.excelRead = getInfo;
return View();
然后将其传递给视图
并用@ViewBag.excelRead
做一个foreach循环
foreach (var data in @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();
}
我在我的 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
如何从所有行中获取值?非常需要这个帮助!谢谢。
将 getDate 设为 .ToList()
var getData = (from a in excelFile.Worksheet(sheetName) select a);
List<string> getInfo = new List<string>();
foreach (var a in getData)
{
getInfo.Add("Name: "+ a["Name"] +"; Amount: "+ a["Amount"] +">> ");
}
ViewBag.excelRead = getInfo;
return View();
然后将其传递给视图 并用@ViewBag.excelRead
做一个foreach循环 foreach (var data in @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();
}