【问题标题】:How to create google-drive spreadsheet from .csv file?如何从 .csv 文件创建 google-drive 电子表格?
【发布时间】:2012-09-05 12:38:15
【问题描述】:

我想从 .csv 文件创建电子表格。 代码:

static void Main()
        {
            String CLIENT_ID = "<MY ID>";
            String CLIENT_SECRET = "<MY SECRET>";

            var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description, CLIENT_ID, CLIENT_SECRET);
            var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthorization);
            var service = new DriveService(auth);

            File body = new File();
            body.Title = "My spread";
            body.Description = "A test spread";
            body.MimeType = "application/vnd.google-apps.spreadsheet";

            byte[] byteArray = System.IO.File.ReadAllBytes("spread.csv");
            System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);

            service.Files.Insert(body, stream, "application/vnd.google-apps.spreadsheet").Upload();
        }

        private static IAuthorizationState GetAuthorization(NativeApplicationClient arg)
        {
            string[] scopes = new string[] { "https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/userinfo.profile" };
            IAuthorizationState state = new AuthorizationState(scopes);
            state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
            Uri authUri = arg.RequestUserAuthorization(state);

            // Request authorization from the user (by opening a browser window):
            Process.Start(authUri.ToString());
            Console.Write("  Authorization Code: ");
            string authCode = Console.ReadLine();
            Console.WriteLine();

            // Retrieve the access token by using the authorization code:
            return arg.ProcessUserAuthorization(authCode, state);
        }

插入文件并在 google-drive 页面上单击它后(登录为所有者)我收到消息

“我们很抱歉。 找不到此 URL 处的电子表格。确保您拥有正确的 URL,并且电子表格的所有者没有将其删除”

当我将 MimeType 更改为“text/csv”并插入文件时,单击它后,我会收到消息 “没有可用的预览 此项目是使用 (MyApp'sName)(一个 Google Drive 应用程序)创建的。 下载此文件或使用您已安装的应用程序之一打开它。”

我也可以右键单击此文件(使用“text/csv”MimeType 创建的文件)并选择选项“导出到 Google Docs”,这给了我想要的结果到达 - 包含我的 .csv 文件内容的电子表格文件。但是这种间接的方法并不能完全满足我。有什么方法可以在谷歌驱动器上制作电子表格文件,内容来自我的应用程序中的 .csv 文件?

【问题讨论】:

  • 任何带有完整源代码示例的最终解决方案?

标签: c# .net google-drive-api


【解决方案1】:

我不知道c#,但在此基础上它反映了Java,在这一行中

service.Files.Insert(正文, 流, "application/vnd.google-apps.spreadsheet").Upload();

你需要插入等价的

.setConvert(true)

还有……

mimetype 应该是“text/csv”

【讨论】:

    【解决方案2】:

    我找到了答案:)

    如何以编程方式将文件转换为适当的 Google 文档格式:

    var service = new DriveService(new BaseClientService.Initializer
    {
        ...
    });
    
    ...
    
    FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, _mimeType);
    request.Convert = true;
    request.Upload();
    

    【讨论】:

      猜你喜欢
      • 2012-12-16
      • 2015-07-22
      • 1970-01-01
      • 2021-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多