Save excel file programmatically from webpage

I have a web page that displays a report. When I click on the link for Excel, it puts all the data displayed on the screen into a CSV file and opens a file download window that allows me to open, save, or cancel.

I want to download this file to a specified location programmatically if the user doesn't see the file download window. I am using a windows app. I don't have the code for the website displaying this report, so it doesn't have a dataset. Also, I looked at the HTML that is generated when the view source is created on the page, but I cannot clear it to get the data.

Hence, I need to know how to download the excel file from the given site to a location on my computer.

Thanks in advance

Rita

+1


source to share


3 answers


Well, you need to find a related link or send a message. Then you can 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's a little trickier if you need to authenticate, etc.

+4


source


I am assuming you are displaying the webpage in a WebBrowser control. You can handle the WebBrowser.Navigating Event and check the WebBrowserNavigatingEventArgs value of the URL to see if string.EndsWith (".csv", CurrentCultureIgnorecase) (or perhaps a more specific test).

If you find that the link has been clicked for the file you want to download, you can call e.Cancel () = True on the WebBrowserEventArgs instance to stop the browser from doing its normal behavior when opening the file Download dialog. Then you can use .NET HttpWebRequest and HttpWebResponse to manually download the file.



Here's an example of loading using these classes

0


source


Actually I am doing screen scraping so I am not displaying the webpage.

What I did disappear from the url where the report is displayed on the screen. There is a hyperlink for "Excel". I am making this hyperlink by clicking my program itself. Clicking this link opens the File Download dialog box.

I wrote, WebBrowser.Navigating and Navigated events, but the control doesn't come here. Any ideas

One more thing, the site I am referring to is the java website.

0


source







All Articles