2012-10-15 30 views
1

我做進口從Excel工作表到SQL database..everything數據是好的,當我運行這段代碼,我訪問引擎找不到MT票據,它會引發這樣的錯誤.. 我的錯誤是它爲什麼會拋出這樣的錯誤?

Microsoft Jet數據庫引擎找不到對象'Sheet1 $'。確保對象存在,並且正確拼寫其名稱和路徑名。

,但我已經與我的指定文件夾中檢查,其correct..then我不知道爲什麼它重複

低於我的C#代碼..

public partial class _Default : System.Web.UI.Page 
{ 
    string constr = @"Data Source=VIS1-B12\SQLEXPRESS;Initial Catalog=Sql_Excel;Integrated Security=True providerName=System.Data.SqlClient" ; 

    protected void btn_okClick(object sender, EventArgs e) 
    { 
     string path = Fup_Excel.PostedFile.FileName; 
     string exconstr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+path+";Extended Properties=Excel 8.0"; 

     OleDbConnection excelcon = new OleDbConnection(exconstr); 
     excelcon.Open(); 

     OleDbCommand cmd = new OleDbCommand("select * from [Sheet1$]", excelcon); 
     OleDbDataReader dbreader; 
     OleDbDataAdapter dap = new OleDbDataAdapter(cmd); 
     DataSet ds = new DataSet(); 
     //dap.Fill(ds,"sheet1"); 
     dbreader = cmd.ExecuteReader(); 

     SqlBulkCopy bcpy = new SqlBulkCopy(constr); 
     bcpy.DestinationTableName = "Excel_Details"; 
     bcpy.WriteToServer(dbreader); 
     //GridView1.DataSource = ds.Tables[0].DefaultView; 
     //GridView1.DataBind(); 
     excelcon.Close(); 
    } 
} 
+0

打開* *的Excel文件和驗證片的名稱。重要的是,您必須在開始閱讀之前複製上傳的文件。 – adatapost

+0

表名是-Sheet1 – Ananth

+0

不,我用正確的文件檢查只有 – Ananth

回答

0

它的更好,你應該檢索Excel表格名稱使用代碼。
代碼如下所示

OleDbConnection con = new OleDbConnection(ConnString); 
OleDbCommand cmd = new OleDbCommand(); 
cmd.CommandType = System.Data.CommandType.Text; 
cmd.Connection = con; 
OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmd); 
DataTable dtExcelRecords = new DataTable(); 
con.Open(); 
DataTable dtExcelSheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); 
string getExcelSheetName = dtExcelSheetName.Rows[0]["Table_Name"].ToString(); 
cmd.CommandText = "SELECT * FROM [" + getExcelSheetName + "]"; 
dAdapter.SelectCommand = cmd; 
dAdapter.Fill(dtExcelRecords); 
+0

這也是在dAdapter.Fill(dtExcelRecords) – Ananth

+0

@ user1746934引發錯誤:你哪個錯誤得到? – ravidev