【问题标题】:How to convert office files (docx, xlsx) to google format in c#如何在 C# 中将 office 文件(docx、xlsx)转换为 google 格式
【发布时间】:2017-08-21 22:40:20
【问题描述】:

任何人分享将docx,xl​​sx,pptx以编程方式转换为google格式的方法。我正在通过 Google Drive API 上传并使用 c# 分享给用户。每次有人编辑文件时都会创建一个重复的文件。

我想在上传时转换文件。所以我可以把转换后的文件分享给大家。

我正在使用 Google Drive v3 api。

下载代码:

private static System.IO.MemoryStream DownloadFile(DriveService service, string fileID)
    {
        var request = service.Files.Get(fileID);
        var stream = new System.IO.MemoryStream();

        // Add a handler which will be notified on progress changes.
        // It will notify on each chunk download and when the
        // download is completed or failed.
        request.MediaDownloader.ProgressChanged += (Google.Apis.Download.IDownloadProgress progress) =>
        {
            switch (progress.Status)
            {
                case Google.Apis.Download.DownloadStatus.Downloading:
                    {
                        Console.WriteLine(progress.BytesDownloaded);
                        break;
                    }
                case Google.Apis.Download.DownloadStatus.Completed:
                    {
                        Console.WriteLine("Download complete.");
                        //SaveStream(stream, saveTo);
                        break;
                    }
                case Google.Apis.Download.DownloadStatus.Failed:
                    {
                        Console.WriteLine("Download failed.");
                        break;
                    }
            }
        };
        request.Download(stream);
        return stream;
    }

    private static void SaveStream(System.IO.MemoryStream stream, string saveTo)
    {
        using (System.IO.FileStream file = new System.IO.FileStream(saveTo, System.IO.FileMode.Create, System.IO.FileAccess.Write))
        {
            stream.WriteTo(file);
        }
    }

【问题讨论】:

    标签: c# google-drive-api google-docs


    【解决方案1】:

    这里有一些东西可以让你开始我的朋友。检查Basic uploadsImporting to Google Docs types,因为它向您展示了如何上传某些文件类型并将其转换为 Google 格式。

    var fileMetadata = new File()
    {
        Name = "My Report",
        MimeType = "application/vnd.google-apps.spreadsheet"
    };
    FilesResource.CreateMediaUpload request;
    using (var stream = new System.IO.FileStream("files/report.csv",
                            System.IO.FileMode.Open))
    {
        request = driveService.Files.Create(
            fileMetadata, stream, "text/csv");
        request.Fields = "id";
        request.Upload();
    }
    var file = request.ResponseBody;
    Console.WriteLine("File ID: " + file.Id);
    

    【讨论】:

    • 谢谢,工作很好...,但是通过 api 下载时,文件没有在 MS Word 或 Excel 中打开... 下载时有什么需要更改的吗?附上我的问题的下载代码...
    • 我已经通过使用导出选项做到了这一点。 var request = service.Files.Export(fileID, GetMimeType(saveTo));//service.Files.Get(fileID);谢谢...
    猜你喜欢
    • 1970-01-01
    • 2021-01-27
    • 2015-05-27
    • 2011-06-14
    • 2015-02-01
    • 2016-09-06
    • 2017-09-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多