簡體   English   中英

如何在C#中將Office文件(docx,xl​​sx)轉換為Google格式

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

任何人都可以通過編程方式將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);
        }
    }

這可以幫助您開始我的朋友。 檢查基本上傳導入到Google文檔類型,因為它顯示了如何上傳和轉換某些文件類型為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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM