asp.net中的文件路径错误无效

时间:2009-07-17 12:55:56

标签: .net asp.net file

我使用一个简单的FileUpload控件来选择一个excel文件,检索数据并将其存储到db。当我在选择文件后尝试上传时,我收到此错误。但文件路径是正确的。

  

“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;

3 个答案:

答案 0 :(得分:1)

您是否尝试使用Server.MapPath(FileName)包装文件路径?

如果你Reponse,你的连接字符串是什么样的。把它写到页面上?

答案 1 :(得分: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;
}

答案 2 :(得分: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;
            }
        }
    }
}