- Your Google SpreadSheet (Google Drive, Google Docs) document must be set to "Anyone with the link" can view it
- To get URL press SHARE (top right corner) on Google SpreeadSheet and copy "Link to share".
- Now add
&output=csv
parameter to this link - Your link will look like:
https://docs.google.com/spreadsheet/ccc?key=1234abcd1234abcd1234abcd1234abcd1234abcd1234&usp=sharing&output=csv
-
-
Save jasonleehodges/359eb2af0d1caec0153bb7450c165ffd to your computer and use it in GitHub Desktop.
Download CSV file from Google Spreadsheet (Google Drive) using minimum c# code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Net; | |
namespace ConsoleApplication1 | |
{ | |
class Program | |
{ | |
public class WebClientEx : WebClient | |
{ | |
public WebClientEx(CookieContainer container) | |
{ | |
this.container = container; | |
} | |
private readonly CookieContainer container = new CookieContainer(); | |
protected override WebRequest GetWebRequest(Uri address) | |
{ | |
WebRequest r = base.GetWebRequest(address); | |
var request = r as HttpWebRequest; | |
if (request != null) | |
{ | |
request.CookieContainer = container; | |
} | |
return r; | |
} | |
protected override WebResponse GetWebResponse(WebRequest request, IAsyncResult result) | |
{ | |
WebResponse response = base.GetWebResponse(request, result); | |
ReadCookies(response); | |
return response; | |
} | |
protected override WebResponse GetWebResponse(WebRequest request) | |
{ | |
WebResponse response = base.GetWebResponse(request); | |
ReadCookies(response); | |
return response; | |
} | |
private void ReadCookies(WebResponse r) | |
{ | |
var response = r as HttpWebResponse; | |
if (response != null) | |
{ | |
CookieCollection cookies = response.Cookies; | |
container.Add(cookies); | |
} | |
} | |
} | |
static void Main(string[] args) | |
{ | |
/* | |
1. Your Google SpreadSheet document must be set to 'Anyone with the link' can view it | |
2. To get URL press SHARE (top right corner) on Google SpreeadSheet and copy "Link to share". | |
3. Now add "&output=csv" parameter to this link | |
4. Your link will look like: | |
https://docs.google.com/spreadsheet/ccc?key=1234abcd1234abcd1234abcd1234abcd1234abcd1234&usp=sharing&output=csv | |
*/ | |
string url = @"https://docs.google.com/spreadsheet/ccc?key=1234abcd1234abcd1234abcd1234abcd1234abcd1234&usp=sharing&output=csv"; // REPLACE THIS WITH YOUR URL | |
WebClientEx wc = new WebClientEx(new CookieContainer()); | |
wc.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0"); | |
wc.Headers.Add("DNT", "1"); | |
wc.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); | |
wc.Headers.Add("Accept-Encoding", "deflate"); | |
wc.Headers.Add("Accept-Language", "en-US,en;q=0.5"); | |
var outputCSVdata = wc.DownloadString(url); | |
Console.Write(outputCSVdata); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment