C#MVC如何从本地数据库中的.xlsx文件加载数据 [英] C# MVC how load data from .xlsx file in local database

查看:126
本文介绍了C#MVC如何从本地数据库中的.xlsx文件加载数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨亲爱的C#mvc程序员



请我是C#mvc的新手,有点帮助将数据从csv文件加载到loacal数据库。谢谢:)



Hi Dear C# mvc programmers

please I am new in C# mvc a bit help load data from csv file to loacal database. Thank you :)

CSv file has a value in the first row I have to avoid this value to skip error get loading the data

+-------+------+--------------+
|       | 300  |              |
+-------+------+--------------+
| age   | fname| lastName     |
+-------+------+--------------+
|   1   | Anna |   Smith      |
+-------+------+--------------+
|   2   |  Tom |  Cat         |
+-------+------+--------------+
|   3   |  Sam |   Fear       |
+-------+------+--------------+

class Detail
[Key]
Id {get;set;}
age{get;set;}
Fname{get;set;}
LastName{get;set;}

context 

 public DbSet<Detail> Detail{ get; set; }





我尝试过:



In Home controller





What I have tried:

In Home controller

public ActionResult Upload()
        {
           
            return View(db.Details.ToList());
        }



   [HttpPost]
        public ActionResult Upload(HttpPostedFileBase file)
        {
            if (file != null && file.ContentLength > 0)
            {
                StreamReader csvReader = new StreamReader(file.InputStream);
                string inputLine = "";
                while ((inputLine = csvReader.ReadLine()) != null)
                {
                    string[] values = inputLine.Split(new Char[] { ',' });
                    Detail records = new Detail();
                    records.age= values[0];
                    records.fname= values[1];
                    records.lastname= values[2];
//I am getting error here 'index was out of bounds of the array'
                   
                   db.Details.Add(records);
                    db.SaveChanges();
                    ViewBag.Success = "Data saved";
                }
                csvReader.Close();
            }



            return View("Upload");
        }







Upload.cshtml










Upload.cshtml



@model IEnumerable<AppGarden.Models.Detail>

@{
    ViewBag.Title = "Upload";
}

<h3>Upload File</h3>

@using (Html.BeginForm("Upload",
                    "Home",
                    FormMethod.Post,
                    new { enctype = "multipart/form-data" }))
{
    <div>
        <label for="File">Upload File:</label>
        <input type="file" name="File" id="File"  /><br />

        <input type="submit" value="Upload File" /><br />
    </div>

   

   

}

推荐答案

如果是我,我找到一个已经知道如何解析CSV文件的库,并使用它。简单地用逗号分隔每一行并不总能完成工作。此外,使用适当的数据类型将数据放入数据库是唯一正确的方法。



我写了一个解析Excel和CSV文件的库。你可能想看一下:



CSV / Excel文件解析器 - 重访 [ ^ ]
If it were me, I'd find a library that already knows how to parse CSV files, anduse that. Simply splitting eachline on a comma will NOT always get the job done. Furthermore, putting the data into the database with an appropriate datatype is the only way to do it right.

I wrote a library that parses Excel and CSV files. You might want to check it out:

CSV/Excel File Parser - A Revisit[^]


这篇关于C#MVC如何从本地数据库中的.xlsx文件加载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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