Downloading a file

ddady

Member
Joined
Sep 19, 2006
Messages
20
Programming Experience
Beginner
Hi,

I want to create a button when the user will press it will download a file from a web, of course i would like the user to see that the file is downloaded but i dont want the save/run windows message will appear, i want to decide where it will be save, somthing like update. The button in the application will be the user's confirmation for the download.


Is there a way to get info from an excel file??
 
A quick google reveals the following for downloading the image/file itself.

VB.NET:
Dim client As New WebClient
Dim strURL As String = "http://www.google.com/images/logo_sm.gif"
Dim strFilename As String = "C:\logo.gif"
client.DownloadFile(strURL, strFilename)

Note that you need to import System.Net for that WebClient to work.

For your Excel question, you might start here:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_vsto2003_ta/html/ExcelObj.asp
-or-
http://support.microsoft.com/?kbid=311452
 
Last edited:
Found this code which works perfect:

VB.NET:
Dim wc As New System.Net.WebClient()
wc.DownloadFile("http://www.uni-giessen.de/uni/pic/3-d-raute.gif", _
"c:\corrent.jpg")

Now, what did you mean by saying:

Note that you need to import System.Net for that WebClient to work.

Regarding reading from xls file. I have looked into the link but i couldn't find anything that show how to do it.
 
If you want to read from Excel you'll first need to import this .NET framework library:

Microsoft Excel 11.0 Object Library

Try this example code to get you going:

VB.NET:
Public Shared Sub Test()
    Dim row, col As Int32
    Dim cellref, val As String
    
    'create excel application and hide it
    Dim ex As Application = New ApplicationClass()
    ex.Visible = False
    
    'open the workbook
    Dim wb As Workbook = ex.Workbooks.Open("c:\test.xls")
    
    If wb IsNot Nothing Then
        'default worksheet is item 1
        Dim ws As Worksheet = DirectCast(wb.Worksheets(1), Worksheet)
        
        'loop thru as many rows/cols as you need
        For row = 0 To 9
            For col = 0 To 9
                'build cell reference ie. 0,0 = A1
                cellref = Chr(col+65) & (row+1)
                
                'print value from cell range
                val = DirectCast(ws.Range(cellref), Range).Value
                Console.WriteLine(cellref & vbtab & val)
            Next
        Next
    End If
End Sub

mafro
 
Thanks, but i'm getting numerous of errors:

Error 1 Type 'ApplicationClass' is not defined.
Error 2 'Visible' is not a member of 'System.Windows.Forms.Application'.
Error 3 Type 'Workbook' is not defined.
Error 4 Type 'Worksheet' is not defined.
Error 5 Type 'Range' is not defined.


BTW i have imported the library by going in the menu to "project">add reference>COM tab>Microsoft Excel 11.0 object library.

Is that correct?
 
Ok, i have found out my mistake.

Thanks mafrosis your code is great and works perfect.

I had to write this

VB.NET:
Imports Excel

On the most top line and it worked.

Now, what if i need only specific data from specific cells? how can i choose which cells to get the data from?
 
Sorry for the delay, been out of the orifice for a couple days..

What we are doing in that code is looping through 10 columns by 10 rows, and building a text reference to the cell to retrieve the data. So it's easy to do a specific cell:

VB.NET:
cellref = "B13"
val = DirectCast(ws.Range(cellref), Range).Value

mafro
 
Don't use ApplicationClass, use Excel.Application instead. (Dim x As New Excel.Application), and when you are finished with it you quit the application (x.Quit).
 
Any reason for not using ApplicationClass John?

And ooops, I shouldve added the Excel.Quit() thingy to my example :(
 
ApplicationClass "supports the .NET Framework infrastructure and is not intended to be used directly from your code"
 
Back
Top