简体   繁体   English

以编程方式从网页保存excel文件

[英]Programatically saving a excel file from the web page

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. 当我点击excel的链接时,它会将屏幕上显示的所有数据放入CSV文件,并打开一个文件下载窗口,可以打开,保存或取消。

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. 我正在使用Windows应用程序。 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. 此外,我查看了通过在页面上执行视图源生成的HTML,但我无法通过它来获取数据。

Hence I need to know how to download an excel file from a given website to a location on my computer. 因此,我需要知道如何将excel文件从给定的网站下载到我的计算机上的某个位置。

Thanks in advance 提前致谢

Rita 丽塔

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. 如果您需要进行身份验证等,它会变得有点复杂。

I assume that you're showing the web page within a WebBrowser control. 我假设你在WebBrowser控件中显示网页。 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). 您可以处理WebBrowser.Navigating事件并检查WebBrowserNavigatingEventArgs URL属性的值,以查看string.EndsWith(“。csv”,CurrentCultureIgnorecase)(或者更具体的测试)。

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. 如果检测到已单击的链接是针对要下载的文件,则可以在WebBrowserEventArgs实例上调用e.Cancel()= True,以阻止浏览器继续执行打开“文件下载”对话框的正常行为。 Then you can use .NET's HttpWebRequest and HttpWebResponse classes to manually download the file. 然后,您可以使用.NET的HttpWebRequestHttpWebResponse类来手动下载文件。

Here's a sample of downloading using these classes 以下是使用这些类下载的示例

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. 我所做的是转到屏幕上显示报告的URL。 There is a hyperlink for 'Excel'. “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. 我写过,WebBrowser.Navigating和Navigated事件,但控件没有来到这里。 Any ideas 有任何想法吗

1 more thing, the site that I am accessing is java website. 还有一件事,我访问的网站是java网站。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM