2010-09-03 6 views
1

J'essaie d'importer un fichier Excel en utilisant asp.net et C#. J'ai trouvé un exemple dans VB, mais il utilise quelque chose appelé "Server.MapPath" qui ne résout pas un espace de noms. Je suis sur .NET 4.0, C# et Windows XP. J'ai trouvé un "HttpServerUtility.MapPath", mais je ne sais pas si c'est l'équivalent pour IIS7?Téléchargement de fichiers dans 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 échantillon

fonction protégée ExcelConnection() As

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 

End Function

Répondre

1

Server.MapPath prend une application chemin relatif (en commençant par une ~) et le convertit en un chemin complet sur le disque.
Il est un membre de l'objet Server (une instance de la classe HttpServerUtility), qui peut être trouvé sur HttpContext et sur une page ou un contrôle utilisateur.

Si vous avez déjà un chemin d'accès complet sur le disque, vous n'en avez pas besoin.

1

Les HttpContext et Page cours ont tous deux une propriété nommée Server qui retourne un objet HttpServerUtility (en fait, Page appelle simplement return this.Context.Server).

Par conséquent Server.MapPathestHttpServerUtility.MapPath.

Il prend une chaîne et calcule le chemin du fichier auquel il correspondrait si cette chaîne était un URI relatif soutenu par le mappage prêt à l'emploi entre les URI gérés par l'application et le système de fichiers (si vous Il s'agit de mappages de répertoires virtuels complexes qu'il gère pour vous) avec la fonction supplémentaire que si vous commencez avec un tilde ~, il considère que cela est relatif à la racine de l'application). Il peut échouer avec des valeurs qui ne sont pas relatives à la racine de l'application (commence par ~) ou à la racine du site (commence par /) si vous avez effectué un remappage en cours, mais si vous avez effectué un remappage le long le chemin, ce n'est probablement pas votre problème de toute façon.

Questions connexes