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

时间:2020-12-27 22:40:16

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. 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.

我想以编程方式将此文件下载到指定位置,而无需用户看到“文件下载”窗口。我正在使用Windows应用程序。我没有显示此报告的网站的代码,因此没有数据集。此外,我查看了通过在页面上执行视图源生成的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

3 个解决方案

#1


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.

如果您需要进行身份验证等,它会变得有点复杂。

#2


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).

我假设你在WebBrowser控件中显示网页。您可以处理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. Then you can use .NET's HttpWebRequest and HttpWebResponse classes to manually download the file.

如果您检测到已单击的链接是针对您要下载的文件,则可以在WebBrowserEventArgs实例上调用e.Cancel()= True,以阻止浏览器继续执行打开“文件下载”对话框的正常行为。然后,您可以使用.NET的HttpWebRequest和HttpWebResponse类来手动下载文件。

Here's a sample of downloading using these classes

这是使用这些类下载的示例

#3


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.

我所做的是进入屏幕上显示报告的URL。有一个'Excel'的超链接。我从我的程序本身点击这个超链接。单击此链接,将打开“文件下载”对话框。

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

我写过,WebBrowser.Navigating和Navigated事件,但控件没有来到这里。有任何想法吗

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

还有一件事,我访问的网站是java网站。

#1


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.

如果您需要进行身份验证等,它会变得有点复杂。

#2


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).

我假设你在WebBrowser控件中显示网页。您可以处理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. Then you can use .NET's HttpWebRequest and HttpWebResponse classes to manually download the file.

如果您检测到已单击的链接是针对您要下载的文件,则可以在WebBrowserEventArgs实例上调用e.Cancel()= True,以阻止浏览器继续执行打开“文件下载”对话框的正常行为。然后,您可以使用.NET的HttpWebRequest和HttpWebResponse类来手动下载文件。

Here's a sample of downloading using these classes

这是使用这些类下载的示例

#3


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.

我所做的是进入屏幕上显示报告的URL。有一个'Excel'的超链接。我从我的程序本身点击这个超链接。单击此链接,将打开“文件下载”对话框。

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

我写过,WebBrowser.Navigating和Navigated事件,但控件没有来到这里。有任何想法吗

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

还有一件事,我访问的网站是java网站。