[英]Issue attempting to query an Excel file… “The Microsoft Office Access database engine could not find the object 'Sheet1$'..”
[英]The Microsoft Office Access database engine could not find the object 'PPR_Status_Detailed'
我正在嘗試通過c#asp.net網頁將excel電子表格上傳到sql數據庫。 我不斷收到錯誤消息:“ Microsoft Office Access數據庫引擎找不到對象'PPR_Status_Detailed',請確保該對象存在並且您正確拼寫了它的名稱和路徑名。” 我不明白為什么,我將連接字符串和工作表名稱更改為,但仍然收到此錯誤。 當我打開excel文件,然后嘗試上傳該文件時,它顯示“外部表未采用預期格式”。 我正在使用.xls和.xlxs文件
它在OleDbDataReader dr = oledbcmd.ExecuteReader()處失敗;
碼:
> public partial class Upload : System.Web.UI.Page {
> string strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["PostbankConnectionString"].ConnectionString;
> protected void Page_Load(object sender, EventArgs e)
> {
>
> }
>
> public void importdatafromexcel(string excelfilepath)
> {
> //declare variables - edit these based on your particular situation
> string ssqltable = "PPRS";
> // make sure your sheet name is correct, here sheet name is sheet1, so you can change your sheet name if have different
> string myexceldataquery = "Select * FROM [PPR_Status_Detailed]";
> try
> {
> //create our connection strings
> string sexcelconnectionstring = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelfilepath +
> ";Extended Properties=" + "\"excel 12.0;hdr=yes;\"";
>
> string sclearsql = "TRUNCATE TABLE " + ssqltable;
> SqlConnection sqlconn = new SqlConnection(strConnString);
> SqlCommand sqlcmd = new SqlCommand(sclearsql, sqlconn);
> sqlconn.Open();
> sqlcmd.ExecuteNonQuery();
>
> //series of commands to bulk copy data from the excel file into our sql table
> OleDbConnection oledbconn = new OleDbConnection(sexcelconnectionstring);
> OleDbCommand oledbcmd = new OleDbCommand(myexceldataquery, oledbconn);
> oledbconn.Open();
> OleDbDataReader dr = oledbcmd.ExecuteReader();
>
> SqlBulkCopy bulkcopy = new SqlBulkCopy(strConnString);
> bulkcopy.DestinationTableName = ssqltable;
> //Mapping Table column
>
> bulkcopy.ColumnMappings.Add("Task ID","[Task_ID]");
> bulkcopy.ColumnMappings.Add("PPR Caption", "[PPR_Caption]");
> bulkcopy.ColumnMappings.Add("Project Start Date", "[Project_StartDate]");
> bulkcopy.ColumnMappings.Add("Project End Date", "[Project_EndDate]");
> bulkcopy.ColumnMappings.Add("Current Task", "[Current_Task]");
> bulkcopy.ColumnMappings.Add("User", "[User]");
>
>
>
> sqlcmd.ExecuteNonQuery();
> while (dr.Read())
> {
> bulkcopy.WriteToServer(dr);
> }
>
> oledbconn.Close();
> sqlconn.Close();
>
> }
>
> catch (Exception)
> {
> //handle exception
> }
> }
>
>
> }
>
> protected void Button1_Click(object sender, EventArgs e) {
> string CurrentFilePath = Path.GetFullPath(FileUpload1.PostedFile.FileName);
> importdatafromexcel(CurrentFilePath); } }
代替 :-
OleDbDataReader dr = oledbcmd.ExecuteReader();
嘗試這個 :-
DataSet ds = new DataSet();
DataTable dt = new DataTable();
dt = oledbconn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
OleDbDataAdapter sda = new OleDbDataAdapter(oledbcmd);
sda.Fill(ds);
dt = ds.Tables[0];
要正確讀取excel名稱,請使用以下代碼:-
OleDbConn.Open();
DataTable dt = OleDbConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string tableName = dt.Rows[0]["TABLE_NAME"].ToString();
OleDbCommand OleDbCmd = new OleDbCommand($"SELECT * FROM [{tableName}]" , OleDbConn);
請讓我知道這可不可以幫你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.