0
 protected void imgButtonSearchProperty_Click(object sender, EventArgs e)
 {
    try
    {
        if (FileUpload1.HasFile)
        {
            lblmsg.Visible = false;
            GridView2.DataSource = null;
            GridView2.DataBind();
            GridView2.Visible = false;
            string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
            AppLog.Log("The name of the file is " + FileName);
            string Extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
            AppLog.Log("The name of the extension is " + Extension);
            string FolderPath = ConfigurationManager.AppSettings["FolderPath"];
            AppLog.Log("The name of the FolderPath is " + FolderPath);
            string FilePath = Server.MapPath(FolderPath + "\\" + FileName);
            AppLog.Log("The name of the FilePath is " + FilePath);
            FileUpload1.SaveAs(FilePath);
            Import_To_Grid(FilePath, Extension);
        }
    }
    catch (Exception ex)
    {

    }
}
 private void Import_To_Grid(string FilePath, string Extension)
{
    try
    {
        string conStr = "";
        switch (Extension)
        {
            case ".xls": //Excel 97-03
                conStr = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
                break;
            case ".xlsx": //Excel 07
                conStr = ConfigurationManager.ConnectionStrings["Excel07ConString"].ConnectionString;
                break;
        }
        conStr = String.Format(conStr, FilePath, 1);
        OleDbConnection connExcel = new OleDbConnection(conStr);
        OleDbCommand cmdExcel = new OleDbCommand();
        OleDbDataAdapter oda = new OleDbDataAdapter();
        cmdExcel.Connection = connExcel;
        connExcel.Open();
        DataTable dtExcelSchema;
        dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
        string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
        //dSUploadInventory.Tables.Add(dtExcelSchema);
        connExcel.Close();

        //Read Data from First Sheet
        connExcel.Open();
        cmdExcel.CommandText = "SELECT * From [" + SheetName + "]";
        oda.SelectCommand = cmdExcel;
        oda.Fill(dt);
        connExcel.Close();



        ////To
        string expression = string.Empty;
        expression = "ISNULL(PropertyId,0)=0";
        DataRow[] rows = dt.Select(expression);

        if (rows.Length > 0)
        {
            foreach (DataRow row in rows)
            {
                row.Delete();
                row.AcceptChanges();
            }
        }
        ////To Do
        ViewState["ExcelData"] = dt;



        GridView1.DataSource = dt;
        GridView1.DataBind();
        if (dt.Rows.Count > 0)
        {
            imgButtonSubmit.Visible = true;
        }
    }
    catch (Exception ex)
    {

    }

}

The connection string that I am using in web.config file is :

<add name="Excel03ConString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data      
Source={0};Extended Properties='Excel 8.0;HDR={1}'" /> 
<add name="Excel07ConString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data 
Source={0};Extended Properties='Excel 12.0;HDR={1}'" />

When I am trying to upload the excel using aforementioned code , I am getting the following error :

“External table is not in the expected format.”

I have added the connection string Help would be appreciated.

Zev Spitz
  • 13,950
  • 6
  • 64
  • 136
palak mehta
  • 696
  • 4
  • 13
  • 30
  • 1
    At what point in the code are you getting this error? Also, what are the saved connection strings? Also, have you seen [this question](http://stackoverflow.com/questions/1139390/excel-external-table-is-not-in-the-expected-format?rq=1)? – Zev Spitz Nov 08 '12 at 11:03
  • I have seen the question and I did it in the same manner , but I am still facing the problem !! – palak mehta Nov 08 '12 at 11:09
  • That connection string is appropriate for Excel <=2003 files (because of the Extended Properties value). What about Excel >=2007 (where it needs to be Excel 12.0)? – Zev Spitz Nov 08 '12 at 11:12
  • What is the extension of the file? Is it possible that the file has the wrong extension (i.e. the file is actually Excel 2003 but with a .xlsx extension, or vice versa)? Can you open the file in Excel? – Zev Spitz Nov 08 '12 at 11:18
  • Yes , I can open it it is .xlsx .I have stored data in the file, which I AM TRYING TO UPLOAD but for no avail.I am stuck !! – palak mehta Nov 08 '12 at 11:19

0 Answers0