2009-07-17 60 views
0

我使用一个简单的FileUpload控件来选择一个Excel文件,检索数据并将其存储到数据库。当我尝试上传后选择文件我得到这个错误。但文件路径是正确的。asp.net中无效的文件路径错误

“FilePath”不是有效的路径。请 确定路径名称正确,并且您连接 到该文件 所在

代码服务器使用拼写 是:

<add key="OleDbConnection" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source= FilePath ;Extended Properties=&quot;Excel 8.0;HDR=Yes;IMEX=1&quot;"/> 

    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; 
+1

你应该使用的String.Format,而不是在你的连接字符串替换。 – James 2009-07-17 13:06:35

+0

另外你不需要在你的AppSettings [“OleDbConnection”]属性的末尾添加.ToString(),它总是以字符串形式返回值 – James 2009-07-17 13:09:35

+0

OleDbConnection的值是什么样的? – mattruma 2009-07-18 12:51:41

回答

1

行更改为

string OleDbConnection = ConfigurationManager.AppSettings["OleDbConnection"].ToString().Replace("FilePath", Server.MapPath(fileUpload.PostedFile.FileName)).Trim(); 

你的概率有相对路径,你需要在使用Server.Mappath物理路径

尝试下面的代码,我已经成功地在一个片运行查询使用你的想法上面托管的环境。

private OleDbConnectionStringBuilder BuildXLConnString(string DSource) 
{ 
    OleDbConnectionStringBuilder connBuild = new OleDbConnectionStringBuilder(); 
    connBuild.Provider = "Microsoft.Jet.OLEDB.4.0"; 
    connBuild.DataSource = DSource; 
    connBuild.Add("Extended Properties", "Excel 8.0;IMEX=1;HDR=Yes;"); 
    return connBuild; 
} 
1

你尝试包的文件路径与Server.MapPath(FileName)?

如果您将Reponse写入页面,您的连接字符串是什么样的?

0

你错过了 “使用” 块:

var xlApp = new Excel.ApplicationClass(); 
var 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(); 

var dsExcellData = new DataSet(); 

var oleDbConnectionString = 
    ConfigurationManager.AppSettings["OleDbConnection"].Replace(
     "FilePath", fileUpload.PostedFile.FileName).Trim(); 
var commandText = "Select * from [" + strSheetName + "$]"; 
using (
    var oledbConnection = new OleDbConnection(oleDbConnectionString) 
    ) 
{ 
    oledbConnection.Open(); 
    using (var oledbCommand = new OleDbCommand()) 
    { 
     oledbCommand.Connection = oledbConnection; 
     oledbCommand.CommandText = commandText; 
     { 
      using (var oledbAdapter = new OleDbDataAdapter()) 
      { 
       oledbAdapter.SelectCommand = oledbCommand; 
       oledbAdapter.Fill(dsExcellData); 
       return dsExcellData; 
      } 
     } 
    } 
}