locked
File Extension RRS feed

  • Question

  • User-1499457942 posted

    Hi

      How to get file extension . I want if it is .xls then constring should be 

    @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";";

    else

    @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + srcFile + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";";

    Thanks

    Thursday, July 26, 2018 4:54 PM

Answers

  • User-1171043462 posted

    This way

                filePath = path + Path.GetFileName(postedFile.FileName);
                string extension = Path.GetExtension(postedFile.FileName);
                postedFile.SaveAs(filePath);
     
                string conString = string.Empty;
                switch (extension)
                {
                    case".xls": //Excel 97-03.
                        conString = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
                        break;
                    case".xlsx": //Excel 07 and above.
                        conString = ConfigurationManager.ConnectionStrings["Excel07ConString"].ConnectionString;
                        break;
                }

    Complete reference: Bulk Import (Insert) data from Excel to Database using SqlBulkCopy in ASP.Net MVC

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 26, 2018 5:03 PM