En este video te muestro como leer un archivo de Excel desde C# .Net y guardarlo en una base de datos sin utilizar las librerías de Office.
Utilizaremos una librería creada por el MIT llamada spreadsheetlight
Crear archivo de excel con spreadsheetlight: https://www.youtube.com/watch?v=NlTv8Xv7cW0
Aprende entity framework en 10 minutos: https://www.youtube.com/watch?v=6nT-RjMEG0o
string path = @"C:\turuta\miexcel.xlsx";
SLDocument sl = new SLDocument(path);
using (var db= new pruebaEntities()) {
int iRow = 2;
while (!string.IsNullOrEmpty(sl.GetCellValueAsString(iRow, 1)))
{
string codigo = sl.GetCellValueAsString(iRow, 1);
string nombre = sl.GetCellValueAsString(iRow, 2);
int edad = sl.GetCellValueAsInt32(iRow, 3);
var oMiExcel = new miexcel();
oMiExcel.codigo = codigo;
oMiExcel.nombre = nombre;
oMiExcel.edad = edad;
db.miexcel.Add(oMiExcel);
db.SaveChanges();
iRow++;
}
}
