1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| public ActionResult ExcelUpload()
{
return View();
}
[HttpPost]
public ActionResult UploadExcel(Réception_camions objEmpDetail, HttpPostedFileBase FileUpload)
{
Réception_phosphateEntities objEntity = new Réception_phosphateEntities();
string data = "";
if (FileUpload != null)
{
if (FileUpload.ContentType == "application/vnd.ms-excel" || FileUpload.ContentType == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
{
string filename = FileUpload.FileName;
if (filename.EndsWith(".xlsx"))
{
string targetpath = Server.MapPath("~/DetailFormatInExcel/");
FileUpload.SaveAs(targetpath + filename);
string pathToExcelFile = targetpath + filename;
string sheetName = "Sheet1";
var excelFile = new ExcelQueryFactory(pathToExcelFile);
var empDetails = from a in excelFile.Worksheet<Réception_camions>(sheetName) select a;
// L'erreur ( Invalid cast from 'System.DateTime' to 'System.TimeSpan')se produit dans cette ligne:
foreach (var a in empDetails)
{
if (a.Date_d_arrivée != null )
{
DateTime Date_d_arrivée = (a.Date_d_arrivée);
int Id_qualité = Convert.ToInt32(a.Id_qualité);
int result = PostExcelData(a.Date_d_arrivée, a.heure_d_arrivée, a.Poids_cam,/* myBirthdate,*/ a.Id_cam, a.Id_qualité /*a.PostelCode, a.EmailId*/);
if (result <= 0)
{
data = "Hello User, Found some duplicate values! Only unique employee number has inserted and duplicate values(s) are not inserted";
ViewBag.Message = data;
continue;
}
else
{
data = "Successful upload records";
ViewBag.Message = data;
}
}
else
{
data = a.Date_d_arrivée + "Some fields are null, Please check your excel sheet";
ViewBag.Message = data;
return View("ExcelUpload");
}
}
}
else
{
data = "This file is not valid format";
ViewBag.Message = data;
}
return View("ExcelUpload");
}
else
{
data = "Only Excel file format is allowed";
ViewBag.Message = data;
return View("ExcelUpload");
}
}
else
{
if (FileUpload == null)
{
data = "Please choose Excel file";
}
ViewBag.Message = data;
return View("ExcelUpload");
}
}
public int PostExcelData(DateTime Date_d_arrivée, TimeSpan heure_d_arrivée, Decimal Poids_cam, int Id_cam, int Id_qualité)
{
Réception_phosphateEntities DbEntity = new Réception_phosphateEntities();
var InsertExcelData = DbEntity.usp_InsertNewEmployeeDetails(Date_d_arrivée, heure_d_arrivée, Poids_cam, Id_cam, Id_qualité );
return InsertExcelData;
} |
Partager