我使用一个简单的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