如何在 c# 中将 office 文件(docx、xlsx)转换为 google 格式

How to convert office files (docx, xlsx) to google format in c#

谁分享了以编程方式将 docx、xlsx、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);
        }
    }

我的朋友,这里有一些可以帮助你入门的东西。检查 Basic uploads and Importing 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);