从代码下载csv文件

时间:2012-01-10 21:04:49

标签: c# download

在google insight中,我们可以搜索关键字并点击“下载.csv”文件按钮下载结果。 我将流程编码为运行多个关键字。

当我从google insight下载.csv文件并在记事本中打开时,我会看到此格式。

Web Search Interest: hockey
Alabama (United States); Alaska (United States); Arizona (United States); Arkansas (United States); California (United States)
2004 - present

Interest over time
Week,Alabama,Alaska,Arizona,Arkansas,California
2004-01-04 - 2004-01-10,0,41,9,0,5
2004-01-11 - 2004-01-17,10,29,9,0,5
2004-01-18 - 2004-01-24,5,37,8,0,5
2004-01-25 - 2004-01-31,7,20,8,0,5
2004-02-01 - 2004-02-07,6,31,9,0,5
2004-02-08 - 2004-02-14,8,31,8,0,6
2004-02-15 - 2004-02-21,4,36,9,0,5
2004-02-22 - 2004-02-28,4,27,7,0,5
2004-02-29 - 2004-03-06,10,26,7,0,6
2004-03-07 - 2004-03-13,14,34,15,0,7
2004-03-14 - 2004-03-20,4,58,8,0,5
2004-03-21 - 2004-03-27,5,28,10,0,5
2004-03-28 - 2004-04-03,6,18,8,0,5
2004-04-04 - 2004-04-10,4,31,6,0,5
2004-04-11 - 2004-04-17,4,16,7,0,4
2004-04-18 - 2004-04-24,4,18,6,0,5
2004-04-25 - 2004-05-01,6,20,6,0,4
200

但是当我使用以下代码从代码下载时

byte[] csv = client.DownloadData(url);
File.WriteAllBytes(path, csv);

我得到这种格式:

Web Search Interest: football
Alabama (United States); Wisconsin (United States); Wyoming (United States)
2004 - present

Interest over time
Week    Alabama Wisconsin   Wyoming
2004-01-04 - 2004-01-10 14  10  0
2004-01-11 - 2004-01-17 10  7   7
2004-01-18 - 2004-01-24 10  7   7
2004-01-25 - 2004-01-31 10  7   7
2004-02-01 - 2004-02-07 13  8   7
2004-02-08 - 2004-02-14 9   6   0
2004-02-15 - 2004-02-21 7   5   0
2004-02-22 - 2004-02-28 8   5   0
2004-02-29 - 2004-03-06 5   4   0
2004-03-07 - 2004-03-13 5   5   0
2004-03-14 - 2004-03-20 7   4   6
2004-03-21 - 2004-03-27 8   4   7
2004-03-28 - 2004-04-03 7   5   7
2004-04-04 - 2004-04-10 5   3   8
2004-04-11 - 2004-04-17 7   4   8
2004-04-18 - 2004-04-24 7   5   8
2004-04-25 - 2004-05-01 7   6   6

但我的要求是以第一种格式获取代码,因为它是在需要第一种格式的地方上传的。我的代码有什么问题,为什么格式会改变?

我在按钮后面使用这个网址:

<a title="" onclick="trends.PageTracker.trackSoph('exprt');" class="" href="http://www.google.com/insights/search/overviewReport?q=hockey&amp;geo=US-AL%2CUS-AK%2CUS-AZ%2CUS-AR%2CUS-CA&amp;cmpt=geo&amp;content=1&amp;export=1" id="exportLink"><div style="vertical-align: middle; float: left" class="goog-inline-block fs04img SPRITE_csv"></div><span style="text-decoration: underline; color: white; float: left;">&nbsp;</span>Download as CSV</a>

将值分配给url的代码:

 HtmlElement getDownloadLink = webBrowser1.Document.GetElementById("exportLink");
            if (getDownloadLink != null)
            {
                string link = string.Empty;
                link = getDownloadLink.GetAttribute("href");
                downloadsheet(link, dir + textBox1.Text + filecounter + ".csv");
                filecounter = filecounter + 1;


            }

1 个答案:

答案 0 :(得分:1)

您在断点处的代码中使用export=2,但网站代码中的export=1为<{1}}。