views:

276

answers:

4

i have a URL and when i load it up in a browser it recognizes it as a csv file and pops up excel "do you want to open". I want to do this programatically so i can have a winforms app use that url and parse the csv file directly.

what is the quickest way to do this?


EDIT: i tried using WebClient and i am getting the folowing error:

"The remote server returned an error: (500) Internal Server Error."

A: 

If you have a WinForms app, you can use a System.Net.WebClient to read the data as a string.

It will read the entire csv file as a string, but you can write it out or parse it at will.

David Stratton
+3  A: 

I don't see why something like this wouldn't work (in C#):

// Download the file to a specified path. Using the WebClient class we can download 
// files directly from a provided url, like in this case.

System.Net.WebClient client = new WebClient();
client.DownloadFile(url, csvPath);

Where the url is your site with the csv file and the csvPath is where you want the actual file to go.

ajdams
+1. Essentially my idea, but with a code sample. Good answer.
David Stratton
A: 

If you want to just whip something together I would suggest using a scripting language and some bash. Just use wget or something similar to get the file and some scripting language to parse it. You could even use php to parse it once you had the file because I know that php has the following function which is very nice: http://php.net/manual/en/function.fgetcsv.php

I would suggest doing it this way because it is easier, this will certainly let you parse it easily enough though I don't know what you want to do with it from there but the worlds your oyster.

Robert Massaioli
A: 

The following code works for me but I am running Open Office. I have not tested it with Excel. The hacky bit is to rename the local copy of the file to *.xls so that Windows will launch Excel by default, if you leave the file extension as CSV, Windows will launch Notepad by default.

String url = "http://www.example.com/test.csv";
String localfile = "test.xls";

var client = new WebClient();
client.DownloadFile(url, localfile);

System.Diagnostics.Process.Start(localfile);
Adam Pierce
Ah, I misread the question. Please ignore this answer.
Adam Pierce