Monday 17 September 2012

Upload multiple product in mvc3 using xsl file

     Controller
 [HttpPost]
        public ActionResult Digitalproduct(AddProduct model)
        {
            DiGIFUNTION();
            return View();
        }


  public void DiGIFUNTION()
        {
            string filePath = null;
            foreach (string inputTagName in Request.Files)
            {
                HttpPostedFileBase Infile = Request.Files[inputTagName];
                if (Infile.ContentLength > 0 && (Path.GetExtension(Infile.FileName) == ".xls" || Path.GetExtension(Infile.FileName) == ".xlsx" || Path.GetExtension(Infile.FileName) == ".xlsm"))
                {
                    filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
                                  Path.GetFileName(Infile.FileName));
                    try
                    {
                        if (System.IO.File.Exists(filePath))
                        {
                            System.IO.File.Delete(filePath);
                        }
                        Infile.SaveAs(filePath);
                    }
                    catch (Exception ee)
                    {
                    }

                    //Infile.SaveAs(filePath);
                }
                else
                {
                    ModelState.AddModelError("Error", "Only Excel Format Allowed..");
               
                }

                if (filePath != null)
                {
                    System.Data.OleDb.OleDbConnection oconn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath.ToString() + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES\";");
                    oconn.Open();

                    try
                    {
                        if (oconn.State == System.Data.ConnectionState.Closed)
                            oconn.Open();
                    }
                    catch (Exception ex)
                    {
                        // MessageBox.Show(ex.Message);
                    }

                    dynamic myTableName = oconn.GetSchema("Tables").Rows[0]["TABLE_NAME"];
                    OleDbCommand ocmd = new OleDbCommand("select * from [" + myTableName + "]", oconn);
                    OleDbDataReader odr = ocmd.ExecuteReader();
                    if (odr.HasRows)
                    {
                        while (odr.Read())
                        {

                            AddProduct Varproduct=new AddProduct (); 
                            var RemoveDupProduct = CheckProductName(odr[1].ToString().Trim());
                         //   Varproduct.ProductTitle = odr[1].ToString().Trim();
                            Varproduct.Image = odr[2].ToString().Trim();
                            Varproduct.Description = odr[3].ToString().Trim();
                            Varproduct.featured = Convert.ToInt32(odr[4]);
                            Varproduct.Expert= odr[5].ToString().Trim();
                            try
                            {
                                if (RemoveDupProduct == true)
                                {
                                    ModelState.AddModelError("Error", "Your product will be Added Soon..");
                                   
                                }
                                else
                                {
                                    ModelState.AddModelError("Error", "Product Already Exists");
                                   // Response.Write(" <br/>Product Name " + odr[1].ToString().Trim() + " Already exists <br/>");
                                   
                                }
                              
                            }
                            catch (Exception ee)
                            {
                                Response.Write("Error While uploading==:" + ee.ToString());
                            }
                        }
                    }

                }
            }
        }

No comments:

Post a Comment