C# 上传Excel“;外部表未采用预期格式;

C# 上传Excel“;外部表未采用预期格式;,c#,.net,excel,C#,.net,Excel,我在web.config文件中使用的连接字符串是: 我已经添加了连接字符串 非常感谢您的帮助。您在代码中的哪一点出现此错误?另外,保存的连接字符串是什么?还有,你看到了吗?我看到了这个问题,我也以同样的方式做了,但我仍然面临着这个问题!!该连接字符串适用于Excel=2007(其中需要是Excel 12.0)?文件的扩展名是什么?文件是否可能具有错误的扩展名(即文件实际上是Excel 2003,但扩展名为.xlsx,反之亦然)?你能用Excel打开这个文件吗? protected void

我在web.config文件中使用的连接字符串是:

我已经添加了连接字符串
非常感谢您的帮助。

您在代码中的哪一点出现此错误?另外,保存的连接字符串是什么?还有,你看到了吗?我看到了这个问题,我也以同样的方式做了,但我仍然面临着这个问题!!该连接字符串适用于Excel=2007(其中需要是Excel 12.0)?文件的扩展名是什么?文件是否可能具有错误的扩展名(即文件实际上是Excel 2003,但扩展名为.xlsx,反之亦然)?你能用Excel打开这个文件吗?
 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)
    {

    }

}
<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}'" />
“External table is not in the expected format.”