opening excel file in .net RRS feed

  • Question

  • User-168748142 posted

    I am using vb.net 2008 (.net 3.5) and am trying to open a excel file and then read the content from it. Currently I have hard-coded the directory of the file like "C:\imports\".

    I want the user to be able to locate the file on the computer and automatically open it. I am not sure of how to open this directory without hard-coding this.

    So far this is what I have which works :

    Dim file_nm As String = FileUpload1.FileName
    Dim dir As String = "C:\imports"
    Dim connection As String = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=""" + dir + "\"";" + "Extended Properties=""text;HDR=No;FMT=Delimited"""
    Dim query As String = "SELECT * FROM " + file_nm

    but I want to dynamically just locate the file on the computer and open it. I dont want to hard-code the directory in my code.

    Thank you all..

    Wednesday, October 28, 2009 9:19 AM

All replies

  • User1164098974 posted

    You are trying to open a file from the client computer?  You cannot do this without the user selecting the file.  This is for security reasons.

    Wednesday, October 28, 2009 9:29 AM
  • User-168748142 posted

    Thanks for your response. No, I let the user choose the file in the "Fileupload" control and then I will upload it to a folder "Imports" in my project location and then read it from there.

    I want to know how to upload this file to a folder "Imports" which is inside my project.

    this is what I am trying to do:

    Dim loc_imports = Server.MapPath("\Excimp\Imports")

    FileUpload1.SaveAs(file_loc) -->uploads to the server

    then read the contents from the excel file.


    Wednesday, October 28, 2009 9:38 AM
  • User1164098974 posted

    So you want the path that the file is uploaded to on the server to be relative?  I don't believe you can do this.  I know in my applications, I have had to specify the complete file path on the server.  There may be a server configuration option that would allow you to use relative paths when saving, but I don't know it.

    Wednesday, October 28, 2009 10:23 AM