C# 在ASP.NET中上载文件
我正在尝试使用asp.net和C#导入excel文件。我在VB中找到了一个例子,但它使用的是一个名为“Server.MapPath”的东西,它没有解析为名称空间。我使用的是.NET4.0、C#和WindowsXP。我找到了一个“HttpServerUtility.MapPath”,但我不知道这是否等同于IIS7 C# 样品 作为OleDbCommand的受保护函数ExcelConnection()C# 在ASP.NET中上载文件,c#,asp.net,vb.net,.net-4.0,C#,Asp.net,Vb.net,.net 4.0,我正在尝试使用asp.net和C#导入excel文件。我在VB中找到了一个例子,但它使用的是一个名为“Server.MapPath”的东西,它没有解析为名称空间。我使用的是.NET4.0、C#和WindowsXP。我找到了一个“HttpServerUtility.MapPath”,但我不知道这是否等同于IIS7 C# 样品 作为OleDbCommand的受保护函数ExcelConnection() ' Connect to the Excel Spreadsheet Dim xConnStr A
' Connect to the Excel Spreadsheet
Dim xConnStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("~/ExcelImport.xls") & ";" & _
"Extended Properties=Excel 8.0;"
' create your excel connection object using the connection string
Dim objXConn As New OleDbConnection(xConnStr)
objXConn.Open()
' use a SQL Select command to retrieve the data from the Excel Spreadsheet
' the "table name" is the name of the worksheet within the spreadsheet
' in this case, the worksheet name is "Members" and is coded as: [Members$]
Dim objCommand As New OleDbCommand("SELECT * FROM [Members$]", objXConn)
Return objCommand
结束函数
服务器。映射路径
采用应用程序相对路径(以~
开头),并将其转换为磁盘上的完整路径。它是
Server
对象(HttpServerUtility类的实例)的成员,可以在HttpContext
和页面或用户控件上找到
如果您已经在磁盘上提供了完整路径,则不需要它。HttpContext和
Page
类都有一个名为Server
的属性,该属性返回一个HttpServerUtility
对象(实际上,Page
只调用返回这个.Context.Server
)
因此,Server.MapPath
是HttpServerUtility.MapPath
它接受一个字符串并计算它将对应的文件路径,如果该字符串是一个相对URI,则该URI由应用程序处理的URI和文件系统之间的现成映射支持(如果您有复杂的虚拟目录映射,它会为您处理)添加了一个特性,即如果您以波浪号开始(~
),那么它会认为波浪号是相对于应用程序根的)
如果您在此过程中进行了一些重新映射,那么它可能会失败,因为这些值既不是相对于应用程序根目录(以~
开头)也不是相对于站点根目录(以/
开头),但是如果您在这一过程中进行了一些重新映射,那么这可能不是您关心的问题
' Connect to the Excel Spreadsheet
Dim xConnStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("~/ExcelImport.xls") & ";" & _
"Extended Properties=Excel 8.0;"
' create your excel connection object using the connection string
Dim objXConn As New OleDbConnection(xConnStr)
objXConn.Open()
' use a SQL Select command to retrieve the data from the Excel Spreadsheet
' the "table name" is the name of the worksheet within the spreadsheet
' in this case, the worksheet name is "Members" and is coded as: [Members$]
Dim objCommand As New OleDbCommand("SELECT * FROM [Members$]", objXConn)
Return objCommand