2009-07-17 63 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; 
      } 
     } 
    } 
}