I am doing a project and at this moment I need to import data from a CSV file "Excel" Delimited by commas (,) certain information, I need that by means of a form they can upload the excel and that this excel is automatically imported in the table dbo.Collaborators
I'm using MVC5 and SQL Server 2014.
My view is as follows:
@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 as follows:
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 to insert the comma delimited CSV file by means of this form, and when I upload the information that is in the file, it will be imported into the table Collaborators
corresponding to the model .
I have been reviewing several examples in internet groups and StackOverFlow but I do not give a clear example with regard to what I need, any contribution is quite useful.
It should be noted that I am using MVC5 in visual studio 2015.