Welcome to plsql4all.blogspot.com SQL, MYSQL, ORACLE, TERADATA, MONGODB, MARIADB, GREENPLUM, DB2, POSTGRESQL.

Sunday, 22 June 2014

Import Excel Sheet as DataTable, Read Data from it

private decimal MethodName()
        {
            decimal amount = 0;
            DateTime ExpiryDate;
            try
            {
                if (fuID.HasFile && fuID.PostedFile.ContentLength > 0)
                {
                    string path = fuID.PostedFile.FileName;
                   
                    if (File.Exists(path))
                    {
            //convert excel to datatable

                        System.Data.DataTable dt = Common.ImportExcelXLS(path);
                        if (dt != null)
                        {
                            ViewState["Data"] = dt;
                            foreach (DataRow dr in dt.Rows)
                            {
                               
                                //do something
                            }
                        }
                   }
                }
            }
            catch (Exception ex)
            {
                //throw ex;
            }


            return amount;
            
        }

public static DataTable ImportExcelXLS(string fileLocation)
        {
            DataTable dt = new DataTable();
            DataTable dt1 = new DataTable();
            DataSet ds = new DataSet();
            using (OleDbConnection conn = new OleDbConnection())
            {
                string connectionString = string.Empty;
                try
                {
                    renameWorksheet(fileLocation);
                    if (Path.GetExtension(fileLocation) == ".xlsx")
                    {
                        StringBuilder sbConn = new StringBuilder();
                        sbConn.Append("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=");
                        sbConn.Append(fileLocation);
                        sbConn.Append(";Extended Properties='Excel 8.0;IMEX=1'");
                        connectionString = sbConn.ToString();
                        //"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + ";Extended Properties='Excel 8.0;IMEX=1'";
                    }
                    else
                    {
                        StringBuilder sbConn = new StringBuilder();
                        sbConn.Append("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=");
                        sbConn.Append(fileLocation);
                        sbConn.Append(";Extended Properties='Excel 8.0;IMEX=1'");

                        connectionString = sbConn.ToString(); //"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileLocation + ";Extended Properties='Excel 8.0;HDR=No;IMEX=1'";
                    }
                    conn.ConnectionString = connectionString;
                    if (conn.State != ConnectionState.Open)
                    {
                        conn.Open();
                    }
                    DataTable dtSchema = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

                    string sheetName = GetSheetName(dtSchema);

                    OleDbCommand cmd = new OleDbCommand("select * from [" + sheetName + "]", conn);
                    OleDbDataAdapter da = new OleDbDataAdapter(cmd);
                    dt.TableName = sheetName.Replace("$", "").Replace("'", "");
                    da.Fill(dt);

                    for (int h = 0; h < dt.Rows.Count; h++)
                    {
                        if (dt.Rows[h].IsNull(0) == true)
                        {
                            dt.Rows.Remove(dt.Rows[h]);
                        }
                    }
                }
                catch (Exception ex)
                {

                }
                finally
                {
                    conn.Close();
                }
                return dt;
            }

        }

 public static string GetSheetName(DataTable Dtschema)
        {
            List<string> sheetName = new List<string>();
            for (int i = 0; i < Dtschema.Rows.Count; i++)
            {
                if (!Dtschema.Rows[i].Field<string>("TABLE_NAME").ToString().Trim().ToUpper().Contains("FILTERDATABASE"))
                {
                    sheetName.Add(Dtschema.Rows[i].Field<string>("TABLE_NAME"));
                }
            }
            return sheetName[0].ToString();
        }

install 64bit AccessDataBaseEngine_64x.exe
Download from the following link:
http://www.microsoft.com/en-in/download/details.aspx?id=13255

Installation Steps:

Launching the install of a Microsoft ACE OLEDB Provider on a machine with an Office install other than the current one (e.g. 32 on 64) will cause the install to fail. To have it run properly you need to launch it from a command line with the “/passive” argument specified.
  • To install the Microsoft ACE OLEDB Provider 32-bit on a machine running Office 2010 64-bit:
$> AccessDatabaseEngine.exe /passive
  • To install the Microsoft ACE OLEDB Provider 64-bit on a machine running Office 2010 32-bit:
$> AccessDatabaseEngine_X64.exe /passive

No comments:

Post a Comment

Please provide your feedback in the comments section above. Please don't forget to follow.