tags:

views:

407

answers:

3

Hi,

I have a web page which displays some report. When I click on the link for excel, it puts all the data that is displayed on the screen into a CSV file and opens a File Download window which lets me either Open, Save or Cancel.

I want to download this file to a specified location programatically without the user seeing the File Download window. I am using a Windows Application. I dont have the code for the website displaying this report hence dont have the dataset. Also, I looked into the HTML that gets generated by doing a view source on the page but I can't really scrape through it to get the data.

Hence I need to know how to download an excel file from a given website to a location on my computer.

Thanks in advance

Rita

+1  A: 

Well you'll need to find the appropriate link or post action. You can then use something like:

string url = "http://whatever/.../foo.xsl";
string target = @"c:/excel documents/foo.xsl";
WebClient client = new WebClient();
client.DownloadFile(url, target);

It gets a bit more complicated if you need to authenticate etc though.

Jon Skeet
A: 

I assume that you're showing the web page within a WebBrowser control. You can handle the WebBrowser.Navigating Event and inspect the value of the WebBrowserNavigatingEventArgs URL property to see if string.EndsWith(".csv", CurrentCultureIgnorecase) (or perhaps a more specific test).

If you detect that the link that has been clicked is for a file you wish to download then you can call e.Cancel() = True on the WebBrowserEventArgs instance to stop the browser from proceeding with its normal behavior of opening the File Download dialog. Then you can use .NET's HttpWebRequest and HttpWebResponse classes to manually download the file.

Here's a sample of downloading using these classes

STW
I'd take Jon's advice of using the WebClient class rather than my suggested HttpWebRequest/Response. The WebClient wraps the Request/Response objects and for these scenarios is quite a bit easier to get up and running.
STW
A: 

Actually I am doing a screen scraping, so I am not displaying the web page.

What I have done is gone to the URL where the report is displayed on the screen. There is a hyperlink for 'Excel'. I make this hyperlink click from my program itself. On clicking this link, it opens the File Download dialog box.

I have written, the WebBrowser.Navigating and Navigated events, but the control does not come here. Any ideas

1 more thing, the site that I am accessing is java website.