如何在C#中将Office文件(docx,xl​​sx)转换为Google格式 [英] How to convert office files (docx, xlsx) to google format in c#

查看:86
本文介绍了如何在C#中将Office文件(docx,xl​​sx)转换为Google格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Anyone share the way to convert docx, xlsx, pptx to google format in programmatic. I am uploading through Google Drive API and sharing to users using c#. every time creating a duplicate document when someone edit the file.

我想在上传时转换文件.这样我就可以将转换后的文件分享给所有人.

I want to convert the file while upload. So i can share the converted file to everyone.

我正在使用Google Drive v3 api.

I am using 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);
        }
    }

推荐答案

以下内容可以帮助您认识我的朋友.检查基本上传

Here's something to get you started my friend. Check the Basic uploads and Importing to Google Docs types as it shows you how to upload and convert some file types to Google formats.

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);

这篇关于如何在C#中将Office文件(docx,xl​​sx)转换为Google格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆