如何上传Excel文件并以编程方式将其转换为Google电子表格?

时间:2014-09-18 04:45:57

标签: c# google-apps-script google-drive-api google-spreadsheet-api

我正在编写桌面程序以与Google spreadsheets进行互动,为此我需要上传excel sheet并通过编程方式将其转换为Google spreadsheet。我查了谷歌驱动器和谷歌电子表格API无法找到这样做的方法。如果某人有任何人可以建议这样做,那将会有所帮助。

这是我现在的代码,它正确上传excel文件,但它没有转换为Google电子表格。

            Google.Apis.Drive.v2.Data.File body = new Google.Apis.Drive.v2.Data.File();
            body.Title = "My document";
            body.Description = "A test document";
            body.MimeType = "application/vnd.ms-excel";

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

            FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, "application/vnd.google-apps.spreadsheet");
            request.Convert = true;
            request.Upload();

            Google.Apis.Drive.v2.Data.File file = request.ResponseBody;
            Console.WriteLine("File id: " + file.Id);
            Console.WriteLine("Press Enter to end this process.");
            Console.ReadLine();

1 个答案:

答案 0 :(得分:0)

我使用的是Drive API,下面是我用来转换XLS文件的代码片段

function convert(xlsxFileId, name) { 
  var xlsxBlob = xlsxFileId;
  var file = {
    title: name,
    //Which Drive Folder do you want the file to be placed in
    parents: [{'id':'FOLDER_ID'}],
    key: 'XXXX',
    value: 'XXXX',
    visibility: 'PRIVATE'
  };

  file = Drive.Files.insert(file, xlsxBlob, {
    convert: true
  });
}