Click here to Skip to main content
15,905,504 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hii all i have saved excel file through file uploader control in a folder at server.
now i want to import this file to griedview bt its giving error:
"External table is not in expected format".the code is:

if (excelfileuploader.HasFile)
        {
            string filename = excelfileuploader.FileName;
            excelfileuploader.SaveAs(Server.MapPath("~/Company/Excel/") + filename);
            string conn = ("Provider=Microsoft.Jet.OLEDB.4.0;" +
               ("Data Source=" + Server.MapPath("~/Company/Excel/")+filename+ ";" +
                        "Extended Properties=\"Excel 8.0;\""));
            string SSQL = "SELECT * from [sheet1$]";
            OleDbDataAdapter oleDA = new OleDbDataAdapter(SSQL, conn);

            DataSet ds = new DataSet();
            
           
            try
            {

                oleDA.Fill(ds);
            }
            catch (OleDbException ex)
            {
                throw ex;
            }
            gridproductdetail.DataSource = ds.Tables[0].DefaultView; // or [ ds ]
            gridproductdetail.DataBind();
        }


plz help
Posted

String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
                   "Data Source=" + Server.MapPath("../ExcelData.xls") + ";" +
                   "Extended Properties=Excel 8.0;";
              // Create connection object by using the preceding connection string.
              OleDbConnection objConn = new OleDbConnection(sConnectionString);
              // Open connection with the database.
              objConn.Open();
              // The code to follow uses a SQL SELECT command to display the data from the worksheet.
              // Create new OleDbCommand to return data from worksheet.
              OleDbCommand objCmdSelect =new OleDbCommand("SELECT * FROM ExcelData$", objConn);
         
              // Create new OleDbDataAdapter that is used to build a DataSet
              // based on the preceding SQL SELECT statement.
              OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
              // Pass the Select command to the adapter.
              objAdapter1.SelectCommand = objCmdSelect;
              // Create new DataSet to hold information from the worksheet.
              DataSet objDataset1 = new DataSet();
              // Fill the DataSet with the information from the worksheet.
              objAdapter1.Fill(objDataset1, "XLData");
              // Bind data to DataGrid control.
              GridView1.DataSource = objDataset1.Tables[0].DefaultView;
              GridView1.DataBind();
              // Clean up objects.
              objConn.Close();

.
try this
 
Share this answer
 
v2
Comments
mylogics 5-May-11 9:09am    
it doesnt work.the code is same..
mylogics 5-May-11 9:12am    
in your case i get error as soon as i open the connection..
Monjurul Habib 5-May-11 15:39pm    
edited:code block added.
 
Share this answer
 
Comments
Rubaba 7-May-11 10:04am    
5 for good links.
Monjurul Habib 9-May-11 2:32am    
thank you rubaba.
 
Share this answer
 
Comments
mylogics 6-May-11 1:37am    
hii this is exactly wat is was looking for...thanks...
Rubaba 7-May-11 10:04am    
5 for good link.
hello dear
as i am just a newbie in this field and trying to learn what ever i can
i did implement this concept in one of my project.
not sure but may be there is a problem with the type of the file Code expect you to upload
there are two or may be more type of MS Excel files are there

1. with the extension .xls (MS Excel 2003 may be)
2. with the extension .xlsx (MS Excel 2007 i think)

you can rewrite the code as below : to check if the code working properly or not do the following procedure

open a new website or project in the asp.net (what ever version you are using )
now add the following controls on the Design window :

1. Label1 (set its visible property to false)
2. Label2 (set its visible property to false)
3. FileUpload1
4. a button control with the text SHowData
5. and a gridview1
6. create a new folder in the current directory name it Files


now goto the code window and add the following code there :


MSIL
protected void Button1_Click(object sender, EventArgs e)
    {
        UploadFileAndBind();
    }

    public void UploadFileAndBind()
    {
        //this will check if there is any file in the fileupload control or not.
        if (FileUpload1.HasFile)
        {
            //Fetch the name of the file.
            string strFileName = FileUpload1.PostedFile.FileName;
            
            //Fetch the Extension of the file.
            string extFile = Path.GetExtension(strFileName ).ToString().ToLower();

            
            if (extFile == ".xls" || extFile == ".xlsx")
            {
                FileUpload1.SaveAs(Server.MapPath("~/Files/" + strFileName));
                BindGrid (Server.MapPath ("~/Files/" + strFileName),extFile );
            }
            else
            {
                Label1.Text = "File You Are Uploading is not an Excel File.";
                Label1.Visible = true;
            }
        }
    }

    public void BindGrid(string filePath, string filetype)
    {
        //variable and object creation



        string constr = null;
        string qry = null;

        //check if the file is of MS Excel 2003 or Of MS Excel 2007 and set the Connection on that basis.

        //Connection string for the MS Excel 2003.
        if (filetype.Trim() == ".xls")
        {
            constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
        }
        //connection string for MS Excel 2007.
        else if (filetype.Trim() == ".xlsx")
        {
            constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
        }

        //query to fetch the Excel Sheet.
        qry = "select * from [Sheet1$]";


        OleDbConnection con = new OleDbConnection(constr);
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }
        OleDbCommand cmd = new OleDbCommand(qry,con );
        OleDbDataAdapter odp = new OleDbDataAdapter(cmd );
        DataSet ds = new DataSet();
        odp.Fill(ds);

        if (ds.Tables.Count > 0)
        {
            if (ds.Tables[0].Rows.Count > 0)
            {
                GridView1.DataSource = ds.Tables[0];
                GridView1.DataBind();
            }
        }
        else
        {
            Label2.Text = "No Data To Show";
            Label2.Visible = true;
        }

        odp.Dispose();
        con.Close();
        con.Dispose();

    }


it works for me but even if you got any problem let me know

GOD help Programmers.
best regards
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900