I am doing a project and at this moment I need to import data from an "Excel" CSV file Delimited by commas (,) certain information, I need that through a form they can upload the excel and that said excel is automatically imported into the table dbo.Collaborators
I am using MVC5 and SQLServer 2014.
My view is the following:
@model SI_OldMutual.Models.Collaborators
@{
ViewBag.Title = "UploadPlanta";
}
<h2>UploadPlanta</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-group">
<input class="form-control" type="file" name="planta" /><br />
<input class="btn btn-default" type="submit" value="Cargar" />
</div>
}
Part of my controller is the following:
private SI_OldMutualContext db = new SI_OldMutualContext();
// UploadPlanta
public ActionResult UploadPlanta()
{
return View();
}
//POST UploadPlanta
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult UploadPlanta([Bind(Include = "planta")] Collaborators collaborators)
{
return View();
}
Part of my model:
[Key]
public int CollaboratorID { get; set; }
[Required]
[Display(Name = "Codigo")]
public string codigo { get; set; }
[Display(Name = "Cedula")]
public string cedula { get; set; }
[Display(Name = "Nombres")]
public string nombres { get; set; }
[Display(Name = "Fecha Ingreso")]
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
public DateTime fecha_ingreso { get; set; }
[Display(Name = "Salario Basico")]
[DisplayFormat(DataFormatString = "{0:C0}", ApplyFormatInEditMode = false)]
public decimal salario_basico { get; set; }
[Display(Name = "Salario Cargo")]
[DisplayFormat(DataFormatString = "{0:C0}", ApplyFormatInEditMode = false)]
public decimal salario_cargo { get; set; }
[Display(Name = "Regimen")]
public string regimen { get; set; }
[Display(Name = "Tipo de Contrato")]
public string tipo_contrato { get; set; }
[Display(Name = "Ciudad")]
public string ciudad { get; set; }
Explanation: I need that through said form they can insert the CSV file delimited by commas and at the moment of automatically loading the information that is in said file, it is imported into the table Collaborators
corresponding to the model.
I have been reviewing several examples in internet groups and StackOverFlow but I can't find a clear example regarding what I need, any input is quite useful.
It should be noted that I am using MVC5 in visual studio 2015.
The first thing is to receive the file you have uploaded in the Controller, save it on the server, read it with Linq and store it in the database:
Take into account that when reading the file, each
part[X]
one will be mapping to a CSV column, that part you have to adjust depending on the design of the CSV file.Can be worked in 2 parts
here an example controller
Here is an example of the view
Here is a pretty clear tutorial:
http://www.c-sharpcorner.com/article/upload-files-in-asp-net-mvc-5/
Once you have uploaded the file, with this TSQL statement you can load it into your table
Of course you must adjust it to your file, table and csv structure
Cheers,