0

这是我到目前为止所拥有的:

// calls a new service and authenticate
SpreadsheetsService myService = new SpreadsheetsService("MyDownloader");
myService.setUserCredentials(DownloaderSettings.Default.Username,
    DownloaderSettings.Default.Password);

// get a list of spreadsheets
SpreadsheetQuery query = new SpreadsheetQuery();
SpreadsheetFeed feed = myService.Query(query);

// download them all
foreach (SpreadsheetEntry entry in feed.Entries)
{
    string key = entry.AlternateUri
                      .Content
                      .Substring(entry.AlternateUri.Content.IndexOf("?key="));
    string dlUrl = "https://docs.google.com/feeds/download/spreadsheets/Export"
                   + key
                   + "&exportFormat=" + DownloaderSettings.Default.FileFormat
                   + "&format=" + DownloaderSettings.Default.FileFormat;

    Stream stream = myService.Query(new Uri(dlUrl));

    using (FileStream fstream = new FileStream(entry.Title.ToString() + "."
           + DownloaderSettings.Default.FileFormat),
           FileMode.Create,
           FileAccess.ReadWrite,
           FileShare.ReadWrite))
    {
        stream.CopyTo(fstream);
        fstream.Flush();
    }
}

我想获取我帐户中所有电子表格的列表并将它们全部下载到xlsx. 我的帐户中有大约 10 个电子表格。当我点击调试时,它们没有被创建。

编辑

如果有帮助,截图:http: //i.imgur.com/wyPZ5jz.png

4

2 回答 2

1

可能是这个表情。。

entry.Title.ToString() + "." + DownloaderSettings.Default.FileFormat

没有产生有效的文件名?它可能包含无效符号或类似符号

于 2013-08-05T04:24:46.533 回答
0

按照 Mauricio Gracia 的建议,我发现一些工作表名称包含非法字符。这对我有用。

Stream stream = myService.Query(new Uri(dlUrl));

// declare the file name, replacing invalid characters
string fileName = entry.Title.Text;
string regexSearch = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());
Regex r = new Regex(string.Format("[{0}]", Regex.Escape(regexSearch)));
fileName = r.Replace(fileName, "_");

using (FileStream fstream = new FileStream(fileName +  "." + DownloaderSettings.Default.FileFormat,
    FileMode.Create,
    FileAccess.ReadWrite,
    FileShare.ReadWrite))
{
    stream.CopyTo(fstream);
    fstream.Flush();
}
于 2013-08-05T05:26:35.473 回答