[Solved] Import data from opened excel files [closed]


Try this code:

 OleDbConnection oledbConn = new OleDbConnection();
        try
        {

            string path = HttpContext.Current.Server.MapPath("~/virtual path for your file");

            if (Path.GetExtension(path) == ".xls")
            {
                oledbConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"");
            }
            else if (Path.GetExtension(path) == ".xlsx")
            {
                oledbConn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties="Excel 12.0;HDR=YES;IMEX=1;";");
            }
            oledbConn.Open();
            OleDbCommand cmd = new OleDbCommand(); ;
            OleDbDataAdapter oleda = new OleDbDataAdapter();
            DataSet ds = new DataSet();

            cmd.Connection = oledbConn;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "SELECT * FROM [Sheet1$]";
            oleda = new OleDbDataAdapter(cmd);
            oleda.Fill(ds, "your_temp_table_name");
            foreach (DataRow item in ds.Tables["your_temp_table_name"].Rows)
            {
                ....

1

solved Import data from opened excel files [closed]