Wednesday, 15 March 2017

Read Excel File as a Data Table using OLEDB


 string filepath = string.Empty;
            filepath = "C:\\TEST.xlsx";
            string sSourceConstr = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source= " + filepath + ";  Extended Properties=Excel 8.0";
           
            OleDbConnection sSourceConnection = new OleDbConnection(sSourceConstr);
            //OleDbCommand cmd = new OleDbCommand("SELECT * FROM [CQALRPF$] WHERE [FACNO] =@id", sSourceConnection);
            OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", sSourceConnection);
            //cmd.Parameters.AddWithValue("@id", 523001740206);
           
            OleDbDataAdapter oleda = new OleDbDataAdapter();
            oleda.SelectCommand = cmd;
            DataSet ds = new DataSet();
            oleda.Fill(ds);
            DataTable dt = ds.Tables[0];

No comments:

Post a Comment