【问题标题】:how to upload a excel file in google doc如何在google doc中上传excel文件
【发布时间】:2012-06-13 14:13:54
【问题描述】:

使用C#,想在google doc上上传excel文件。下面的语法用于上传 xls 文件

//use Content-Type: text/csv
     entry.MediaSource = new MediaFileSource("E:\\Emailcontent.xls", "text/csv");

但它不起作用,上传文件转换为 csv 后。但我不想要这种转换。我只想在我的谷歌文档中上传我的 excel 文件。帮助我上传没有转换的 excel 文件。在此先感谢

  string USERNAME = "xxx@gmail.com";
            string PASSWORD = "xxxxx";

            // Start the service and set credentials
            DocumentsService service = new DocumentsService("MyDocumentsListIntegration-v1");
            service.setUserCredentials(USERNAME, PASSWORD);
            Authenticator authenticator = new ClientLoginAuthenticator("TestApi", Google.GData.Client.ServiceNames.Documents, service.Credentials);
            DocumentEntry entry = new DocumentEntry();
            // Set the document title
            entry.Title.Text = "Legal Contract";
            entry.IsSpreadsheet = true;
            // Set the media source
            //entry.MediaSource = new MediaFileSource("E:\\New Microsoft Office Word Document.doc", "application/msword");
            entry.MediaSource = new MediaFileSource("E:\\Emailcontent.xls", "text/csv");
            // Define the resumable upload link
            Uri createUploadUrl = new Uri("https://docs.google.com/feeds/upload/create-session/default/private/full");
            AtomLink link = new AtomLink(createUploadUrl.AbsoluteUri);
            link.Rel = ResumableUploader.CreateMediaRelation;
            entry.Links.Add(link);

            // Set the service to be used to parse the returned entry
            entry.Service = service;

            // Instantiate the ResumableUploader component.
            ResumableUploader uploader = new ResumableUploader();

            // Set the handlers for the completion and progress events
            uploader.AsyncOperationCompleted += new AsyncOperationCompletedEventHandler(OnDone);
            uploader.AsyncOperationProgress += new AsyncOperationProgressEventHandler(OnProgress);

            // Start the upload process
            uploader.InsertAsync(authenticator, entry, new object());

【问题讨论】:

  • 以后,我建议在发布问题之前先阅读 api。我知道有时很容易陷入困境并寻找错误的解决方案,但是通过在发布之前进行一些研究很容易解决这个问题。它肯定会为自己省去一些头痛! :)

标签: api google-api google-docs google-sheets


【解决方案1】:

您将 xls (Excel) 文件作为文本/csv 传递。如果要上传为 xls,请使用

entry.MediaSource = new MediaFileSource("E:\\Emailcontent.xls", "text/csv");    

如果要上传为 xls,请使用

entry.MediaSource = new MediaFileSource("E:\\Emailcontent.xls", "application/vnd.ms-excel"); 

这是我使用的 wikipedia/google 搜索:

http://en.wikipedia.org/wiki/Internet_media_type

【讨论】:

    【解决方案2】:

    为确保文件在上传时不会被转换,您还应该在上传 uri 中附加 ?convert=false

    【讨论】:

      猜你喜欢
      • 2020-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-25
      • 2021-08-03
      • 2017-10-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多