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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
public ActionResult Upload() { return View(); } [HttpPost] public ActionResult Upload(HttpPostedFileBase file) { //ErrorMessages objerrormodel = new ErrorMessages(); try { DataSet ds = new DataSet(); if (Request.Files["file"].ContentLength > 0) { string fileExtension = System.IO.Path.GetExtension(Request.Files["file"].FileName); string filename = Request.Files["file"].FileName + "_" + System.DateTime.Now.ToString("yyyyMMddhhmm"); if (fileExtension == ".xls" || fileExtension == ".xlsx") { string fileLocation = Server.MapPath("~/Content/") + Request.Files["file"].FileName; if (System.IO.File.Exists(fileLocation)) { System.IO.File.Delete(fileLocation); } Request.Files["file"].SaveAs(fileLocation); string excelConnectionString = string.Empty; excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\""; //connection String for xls file format. if (fileExtension == ".xls") { excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\""; } //connection String for xlsx file format. else if (fileExtension == ".xlsx") { excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\""; } //Create Connection to Excel work book and add oledb namespace OleDbConnection excelConnection = new OleDbConnection(excelConnectionString); excelConnection.Open(); DataTable dt = new DataTable(); dt = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); if (dt == null) { return null; } String[] excelSheets = new String[dt.Rows.Count]; int t = 0; //excel data saves in temp file here. foreach (DataRow row in dt.Rows) { excelSheets[t] = row["TABLE_NAME"].ToString(); t++; } OleDbConnection excelConnection1 = new OleDbConnection(excelConnectionString); string query = string.Format("Select * from [{0}]", excelSheets[0]); using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, excelConnection1)) { dataAdapter.Fill(ds); // return Json(ds, JsonRequestBehavior.AllowGet); } } if (fileExtension.ToString().ToLower().Equals(".xml")) { string fileLocation = Server.MapPath("~/Content/") + Request.Files["FileUpload"].FileName; if (System.IO.File.Exists(fileLocation)) { System.IO.File.Delete(fileLocation); } Request.Files["FileUpload"].SaveAs(fileLocation); XmlTextReader xmlreader = new XmlTextReader(fileLocation); // DataSet ds = new DataSet(); ds.ReadXml(xmlreader); xmlreader.Close(); } for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { Connection(); Con.Open(); Con.Execute("Store_Procedure_Name_Here", new { Unique_Code = ds.Tables[0].Rows[i][0].ToString().Trim(), Name_of_Applicant = ds.Tables[0].Rows[i][1].ToString().Trim(), Applicant_Father_Name = ds.Tables[0].Rows[i][2].ToString().Trim(), Applicant_DOB = ds.Tables[0].Rows[i][3].ToString(), Apllicant_Gender = ds.Tables[0].Rows[i][4].ToString().Trim(), Contact_Number = ds.Tables[0].Rows[i][5].ToString().Trim(), Applicant_Current_Address = ds.Tables[0].Rows[i][6].ToString().Trim(), Applicant_Current_Address_Pin = Convert.ToInt32(ds.Tables[0].Rows[i][7]), Applicant_Current_City = ds.Tables[0].Rows[i][8].ToString().Trim(), Applicant_Current_State = ds.Tables[0].Rows[i][9].ToString().Trim(), App_cur_exe_id = ds.Tables[0].Rows[i][10].ToString().Trim(), App_cur_tele_caller = ds.Tables[0].Rows[i][11].ToString().Trim(), Applicant_Permanent_Address = ds.Tables[0].Rows[i][12].ToString().Trim(), Permannent_Address_Pin = Convert.ToInt32(ds.Tables[0].Rows[i][13]), Permanent_Address_City = ds.Tables[0].Rows[i][14].ToString().Trim(), Permanent_Address_State = ds.Tables[0].Rows[i][15].ToString().Trim(), App_per_exe_id = ds.Tables[0].Rows[i][16].ToString().Trim(), App_per_tele_caller = ds.Tables[0].Rows[i][17].ToString().Trim(), PAN_Number = ds.Tables[0].Rows[i][18].ToString().Trim(), Driving_Licence_Number = ds.Tables[0].Rows[i][19].ToString().Trim(), Aadhar_Number = ds.Tables[0].Rows[i][20].ToString().Trim(), Issuing_Authority = ds.Tables[0].Rows[i][21].ToString().Trim(), Reference1_Name = ds.Tables[0].Rows[i][22].ToString().Trim(), Reference1_Contact_Number = ds.Tables[0].Rows[i][23].ToString(), Reference1_Address = ds.Tables[0].Rows[i][24].ToString().Trim(), Reference1_City = ds.Tables[0].Rows[i][25].ToString().Trim(), Reference1_Pin = Convert.ToInt32(ds.Tables[0].Rows[i][26]), Reference1_State = ds.Tables[0].Rows[i][27].ToString(), Reference1_DOB = ds.Tables[0].Rows[i][28].ToString(), Reference1_Father_Name = ds.Tables[0].Rows[i][29].ToString().Trim(), Import_File_Name = filename }, commandType: CommandType.StoredProcedure); Con.Close(); } ViewData["Message"] = "File Is Upload Succesfully."; //ModelState. } } catch (Exception ex) { ViewData["Message"] = ex.Message.ToString(); } // return Content("<script language='javascript' type='text/javascript'>alert('Save Successfully');</script>"); // ViewData["Message"] = "Please Select File and Then Upload ."; // return View(ViewData["Message"] = "svs"); return View(); } |