错误“外部表不是预期的格式”

时间:2014-04-12 09:48:13

标签: asp.net c#-4.0

这是我的代码

protected void btnSubmit_OnClick(object sender, EventArgs e)
    {
         string path = @"C:\Users\Mazen\Desktop\Source\Book1.xlsx";
         String strExcelConn = "Provider=Microsoft.Jet.OLEDB.4.0;"
                                  + "Data Source=" + path + "; "
                                 + "Extended Properties='Excel 8.0;HDR=Yes'";

         OleDbConnection connExcel = new OleDbConnection(strExcelConn);
         OleDbCommand cmdExcel = new OleDbCommand();
         cmdExcel.Connection = connExcel;
         connExcel.Open();
         System.Data.DataTable dtExcelSchema;
         dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
         connExcel.Close();

         DataSet ds = new DataSet();
         string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
         cmdExcel.CommandText = "SELECT ID, Name From [" + SheetName + "]";
         OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter();
         da.SelectCommand = cmdExcel;
         da.Fill(ds);
}

它给出了一个错误如何解决它..如果我改变Jet到ACE所以它给出了一个错误 提供程序未在本地计算机上注册。请帮帮我

1 个答案:

答案 0 :(得分:1)

请尝试以下strExcelConn

String strExcelConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;";