我使用一個簡單的FileUpload控件來選擇一個Excel文件,檢索數據並將其存儲到數據庫。當我嘗試上傳後選擇文件我得到這個錯誤。但文件路徑是正確的。asp.net中無效的文件路徑錯誤
「FilePath」不是有效的路徑。請 確定路徑名稱正確,並且您連接 到該文件 所在
代碼服務器使用拼寫 是:
<add key="OleDbConnection" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source= FilePath ;Extended Properties="Excel 8.0;HDR=Yes;IMEX=1""/>
string OleDbConnection =
ConfigurationManager.AppSettings["OleDbConnection"].Replace("FilePath",
fileUpload.PostedFile.FileName).Trim();
Excel.ApplicationClass xlApp = new Excel.ApplicationClass();
Excel.Workbooks xlWorkBooks = (Excel.Workbooks)xlApp.Workbooks;
Excel.Workbook wb = xlWorkBooks._Open(fileUpload.PostedFile.FileName, Type.Missing, false, Type.Missing, "", "", true, Excel.XlPlatform.xlWindows, "\t", true, false, Type.Missing, true);
string strSheetName = ((Excel.Worksheet)wb.Sheets[1]).Name.ToString();
xlWorkBooks.Close();
xlApp.Quit();
oledbCommand = new OleDbCommand();
oledbAdapter = new OleDbDataAdapter();
DataSet dsExcellData = new DataSet();
oledbConnection = new OleDbConnection(OleDbConnection);
oledbConnection.Open();
oledbCommand.Connection = oledbConnection;
oledbCommand.CommandText = "Select * from [" + strSheetName + "$]";
oledbAdapter.SelectCommand = oledbCommand;
oledbAdapter.Fill(dsExcellData);
return dsExcellData;
你應該使用的String.Format,而不是在你的連接字符串替換。 – James 2009-07-17 13:06:35
另外你不需要在你的AppSettings [「OleDbConnection」]屬性的末尾添加.ToString(),它總是以字符串形式返回值 – James 2009-07-17 13:09:35
OleDbConnection的值是什麼樣的? – mattruma 2009-07-18 12:51:41