user3844707
user3844707

Reputation: 23

How to convert office files (docx, xlsx) to google format in 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.

I am using Google Drive v3 api.

Download Code:

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

Upvotes: 0

Views: 1088

Answers (1)

ReyAnthonyRenacia
ReyAnthonyRenacia

Reputation: 17651

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

Upvotes: 1

Related Questions