我想用asp.net和C#導入一個excel文件。我在VB中找到了一個例子,但它使用了一種名爲「Server.MapPath」的東西,它不能解析爲名稱空間。我在.NET 4.0,C#和Windows XP上。我發現了一個「HttpServerUtility.MapPath」,但我不知道這是否與IIS7等效?在ASP.NET中上傳文件
C#
public OleDbCommand ExcelConnection()
{
string conStr = "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
OleDbConnection ocnct = new OleDbConnection(conStr);
ocnct.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$]
OleDbCommand ocmd = new OleDbCommand("SELECT * FROM [Members$]", ocnct);
return ocmd;
}
Online樣品
保護的函數ExcelConnection()作爲的OleDbCommand
' 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
端功能