读取CSV文件 [英] Reading in a CSV file

查看:98
本文介绍了读取CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我是C#和Visual studio的新手我正在尝试将.csv文件读入我的Web应用程序MVC项目中,并且无法找到正确的教程我尝试了多种不同的方法并继续获得相同的错误相同的代码片段 下面列出的
是我的代码。 

Hi i am new to C# and visual studio i am trying to read a .csv file into my Web Application MVC project and can not find the correct tutorials i have tried multiple different ways and keep getting the same errors on the same pieces of code  listed below is the code I have. 

[System.Web.Mvc.HttpPost]
        public ActionResult Index(HttpPostedFileBase postedFile)
        {
            List<TeamModel> team = new List<TeamModel>();
            string filePath = string.Empty;
            if (postedFile != null)
            {
                string path = Server.MapPath("~/Uploads/");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                filePath = path + Path.GetFileName(postedFile.FileName);
                string extension = Path.GetExtension(postedFile.FileName);
                postedFile.SaveAs(filePath);

                //Read the contents of CSV file.
                string csvData = System.IO.File.ReadAllText(filePath);

                //Execute a loop over the rows.
                foreach (string row in csvData.Split('\n'))
                {
                    if (!string.IsNullOrEmpty(row))
                    {
                        team.Add(new TeamModel
                        {
                            ID = Convert.ToInt32(row.Split(',')[0]),
                            player_name = row.Split(',')[1],
                            quality = row.Split(',')[2],
                            overall = row.Split(',')[2],
                            club = row.Split(',')[2],
                            league = row.Split(',')[2],
                            nationality = row.Split(',')[2],
                            position = row.Split(',')[2],
                            age = row.Split(',')[2],
                        });
                    }
                }
            }

            return View(team);
        }



  1. 错误 CS0246类型或命名空间名称'找不到HttpPostedFileBase'(您是否缺少using指令或汇编引用?)
  2. 错误 CS0103
    当前上下文中不存在"服务器"名称
  1. Error CS0246 The type or namespace name 'HttpPostedFileBase' could not be found (are you missing a using directive or an assembly reference?)
  2. Error CS0103 The name 'Server' does not exist in the current context

这些是我得到的错误,有人可以帮助我,我发现的所有教程都是针对VS 2010,2015 2017年没有一个不确定是否有所作为。我只是想从项目文件夹中读取一个csv文件并将其放入List<>
i不需要上传文件 

these are the errors I am getting can someone please help me all the tutorials I found was for VS 2010, 2015 none for 2017 not sure if that makes a difference. I am just trying to read a csv file from inside the project folder and put it in a List<> i do not need to upload the file 

这是我获取代码的地方:

This is where I got the code:

https://www.aspsnippets。 com / Articles / Upload-Read-and-Display-CSV-file-Text-file-data-in-ASPNet-MVC.aspx

https://www.aspsnippets.com/Articles/Upload-Read-and-Display-CSV-file-Text-File-data-in-ASPNet-MVC.aspx




推荐答案

AlexGjnr,

Hi AlexGjnr,

欢迎来到MSDN论坛。

Welcome to the MSDN forum.

请参阅您的描述,此问题是关于ASP.NET MVC开发的,我们的论坛是讨论VS IDE,请重定向到这个适当的论坛:  https://forums.asp.net/ 寻求更专业的
支持,感谢您的理解。

Refer to your description, this issue is about the ASP.NET MVC development and our forum is to discuss the VS IDE, please redirect to this appropriate forum: https://forums.asp.net/ to seek for a more professional support, thank you for your understanding.

祝你好运,

Sara


这篇关于读取CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆