using System; using System.Collections; using System.Data; using System.Data.OleDb; /// /// Summary description for dbconnection /// public class Excelhelper { #region Variable Declaration DataSet ds; OleDbDataAdapter adptr; OleDbConnection con = null; String connString = null; #endregion public Excelhelper() { } /// /// connection path for excelsheet /// /// public Excelhelper(string filename) { connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + ";Extended Properties='Excel 12.0 xml;IMEX=1;HDR=YES;TypeGuessRows=0;ImportMixedTypes=Text'"; //connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filename + ";Extended Properties=\"Excel 8.0;IMEX=1;HDR=YES;MAXSCANROWS=0;\""; //connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + ";Extended Properties=Excel 12.0;"; //this.connString = connString; //connString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "datasource = " + filename + ";Extended Properties=Excel 12.0;"; con = new OleDbConnection(connString); } /// /// methord for getconnectinon excel file /// /// //public OleDbConnection getconnection() //{ //connString= ConfigurationManager.AppSettings["Provider"] + "Data Source=" + ConfigurationManager.AppSettings["DataSource"]; // //connString=" Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\Users\\anupc\\Desktop\\ExcelProject\\App_Data\\PricingDatabase.mdb;Persist Security Info=True"; // con=new OleDbConnection( connString ); // con.Open(); // return con; //} //public void closeconnection() //{ // connString = ConfigurationManager.AppSettings["Provider"] + "Data Source=" + ConfigurationManager.AppSettings["DataSource"]; // con = new OleDbConnection(connString); // con.Close(); //} /// /// pic data from excelsheet /// /// /// public DataSet GetDataFromSheet(string query) { try { if (con.State != ConnectionState.Open) con.Open(); adptr = new OleDbDataAdapter(query, con); ds = new DataSet(); adptr.Fill(ds); return ds; } catch { throw new Exception(); } finally { con.Close(); } } /// /// Public Method Use to Get Excel Sheet Names /// /// DataTable public ArrayList GetExcelSheetNames(string UploadFileName) { try { con = new OleDbConnection(connString); if (con.State != ConnectionState.Open) con.Open(); DataTable dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); String[] excelSheets = new String[dt.Rows.Count]; int increment = 0; ArrayList arrList = new ArrayList(); foreach (DataRow row in dt.Rows) { excelSheets[increment] = row["TABLE_NAME"].ToString(); arrList.Add(excelSheets[increment]); increment++; } return arrList; } catch (Exception ex) { Console.Write(ex.Message); return null; } finally { con.Close(); } } }