Skip to content

Instantly share code, notes, and snippets.

@dhlavaty
Last active June 10, 2023 14:08
Show Gist options
  • Save dhlavaty/6121814 to your computer and use it in GitHub Desktop.
Save dhlavaty/6121814 to your computer and use it in GitHub Desktop.
Download CSV file from Google Spreadsheet (Google Drive) using minimum c# code
- This GIST is outdated. I recommend a different approach here: https://gist.github.com/dhlavaty/9cb9f50aed62320329636e805e654eae

Download CSV file from Google Spreadsheet using c# and minimal code

  1. Your Google SpreadSheet (Google Drive, Google Docs) 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
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);
}
}
}
@thatusualguy
Copy link

thatusualguy commented Nov 8, 2021

Discovered another similar way. You can also use https://docs.google.com/spreadsheets/export?id=${my_key}&exportFormat=${format}.

Also you can shrink the code down to

using var client = new WebClient();
client.Headers.Add("accept", "*/*");
byte[] outputData= client.DownloadData(url);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment