Tuesday 15 September 2015

Read the Excel file

Config File :-      <add key ="Excel07ConString"  value="Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; Extended Properties='Excel 12.0 Xml;HDR=YES';"/>


    <add key="sourceDirPath" value="D:\BIO10_DATA\PRD\SAP2INET\EmployeeMaster" />



string strSourcedirPath = ConfigurationManager.AppSettings["sourceDirPath"].ToString();

 try
            {
                FileInfo f = new FileInfo(ExcelFilePath);
                string FileName = Path.GetFileName(f.Name);
                string Extension = Path.GetExtension(f.Name);

                string conStr = "";
                switch (Extension)
                {
                    case ".xls": //Excel 97-03
                        conStr = ConfigurationManager.AppSettings["Excel03ConString"].ToString();
                        break;
                    case ".xlsx": //Excel 07
                        conStr = ConfigurationManager.AppSettings["Excel07ConString"].ToString();                  
                        break;
                }
                //string ConnStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ExcelFilePath + "; Extended Properties='Excel 12.0 Macro;HDR=NO';";
                conStr = String.Format(conStr, ExcelFilePath);


                //OleDbConnection con = new OleDbConnection(conStr);
                //string strquery = "select * from [Sheet1$]";
                //OleDbDataAdapter oledbDA = new OleDbDataAdapter(strquery, con);
                //DataTable dt = new DataTable();
                //oledbDA.Fill(dt);



                //OleDbConnection connExcel = new OleDbConnection(conStr);
                //OleDbCommand cmdExcel = new OleDbCommand();
                //OleDbDataAdapter oda = new OleDbDataAdapter();
                //DataTable dtExcel = new DataTable();
                //cmdExcel.Connection = connExcel;
                //connExcel.Open();
                //DataTable dtExcelSchema;
                //dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                //string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();          
                //cmdExcel.CommandText = "SELECT * From [" + SheetName + "]";
                //oda.SelectCommand = cmdExcel;
                //oda.Fill(dtExcel);

                OleDbConnection con = new OleDbConnection(conStr);
                //string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ExcelFilePath + ";Extended Properties='Excel 12.0 Xml;HDR=YES'";
                //OleDbConnection con = new OleDbConnection(connectionString);
                string strquery = "select * from [Sheet1$]";
                OleDbDataAdapter oledbDA = new OleDbDataAdapter(strquery, con);
                DataTable dt = new DataTable();
                oledbDA.Fill(dt);

  }
            catch (Exception e)
            {
                Console.WriteLine(e.Message.ToString());
                Console.ReadKey();
            }