-1

I'm making a console application in .NET framework that uploads a .XLSX file to Google Drive (converting it to Google Sheets format in that process) and also downloads the Google Sheets to .XLSX format.

Basically I'm trying to sync a local drive with Google Drive, preserving all formatting of the sheets.

I've used the Google Drive API for .NET framework to do this and it is working pretty well, except that when downloading Google Sheets to a .XLSX file, the .XLSX file downloaded is opened without any problem by Excel 2007 but shows the following error when opened with Excel 2016:

We found a problem with some content in the file. Do you want us to try to recover as much as we can?

On clicking "Yes", Excel opens the file successfully, but shows a message with an error log. I opened the error log, and there were no details in it except a mention of "Conditional Formatting". Further, a few freeze rows in the original Google Sheets were not freezed anymore in Excel 2016, but were intact when the file was opened in Excel 2007.

Do you have any suggestions to make the .XLSX file downloaded from Google Sheets compatible with Excel 2016, or a totally different approach that will eliminate the problem with different versions and different formats?

nvkrj
  • 1,002
  • 1
  • 7
  • 17
  • 1
    Does the same problem occur if you use the Drive UI to upload the file and convert it and then download it? If the same problem does occur then report a problem to Google via Google Sheets > Help > Report a problem. If the same problem does not occur, update your question here with the code you're using to upload/convert/download. – Peter Nov 01 '16 at 22:22
  • @Peter: My program uploads the excel file (.xlsx) to Google Drive (thus converting it to Google Sheets format). After user has updated the Google Sheets, it downloads the files to the local drive. Problem is, after downloading, if the file is opened in Excel 2016, it shows a warning message that content was recovered. – nvkrj Nov 02 '16 at 05:47
  • No suggestions but it's worth reporting to Google. – Peter Nov 02 '16 at 06:16
  • I'll do that. Thanks :) I think it's a problem of compatibility between Excel 2016, Excel 2007 and Google Sheets. The file downloaded from Google Sheets opens without any problem in 2007, but shows warnings in 2016. – nvkrj Nov 02 '16 at 07:46

1 Answers1

0

Since this is an old post you probably fixed your problem already. But for future reference i'll try to answer your question using v3 of the Google Drive api.

Google Drive uses it's own MIME Types. A list of these MIME Types can be found here.

Google Drive MIME can be converted in their corresponding MIME type. A list of corresponding MIME types can be found on the bottom of this page.

So if you want to convert a Google you have to find out what MIME type it is and what the corresponding MIME type is.

public FileResult Download(string fileId, string fileName, string mimeType)
{
        DriveService service = new DriveService(new BaseClientService.Initializer
        {
            HttpClientInitializer = <user_credential>
            ApplicationName = "APPNAME"
        });

        string convertedMimeType = "";
        bool googleDoc = true;

        switch (mimeType)
        {
            case "application/vnd.google-apps.document":
            {
                convertedMimeType = "application/vnd.oasis.opendocument.text";
                break;
            }
            case "application/vnd.google-apps.spreadsheet":
            {
                convertedMimeType = "application/x-vnd.oasis.opendocument.spreadsheet";
                break;
            }
            case "application/vnd.google-apps.drawing":
            {
                convertedMimeType = "image/png";
                break;
            }
            case "application/vnd.google-apps.presentation":
            {
                convertedMimeType = "application/pdf";
                break;
            }
            default:
            {
                convertedMimeType = mimeType;
                googleDoc = false;
                break;
            }
        }

        MemoryStream stream = new MemoryStream();

        if (googleDoc)
        {
            var request = service.Files.Export(fileId, convertedMimeType);
            request.Download(stream);
        }
        else
        {
            service.Files.Get(fileId).Download(stream);
        }

        return File(stream.ToArray(), convertedMimeType, fileName);
}

You can get the MIME type of a file by using:

Google.Apis.Drive.v3.Data.File file = new Google.Apis.Drive.v3.Data.File(..);
string type = file.MimeType;

In the switch case I check if the MIME type is a Google Document MIME type. If it is, then I set the MIME type to the corresponding MIME type of the Google Document. If it is not a Google Document, you can use the MIME type you just got from the file. Google Drive won't change the MIME type if you upload a file that is not a Google Document.

Please not that the above sample is used in a ASP.NET MVC project.

MetalJacketNL
  • 109
  • 2
  • 11
  • Hi MennoB! Thanks for your answer, but that's exactly what I was doing. The program was downloading *only* Google Sheets files and saving them as Excel files, so comparing mime types did not matter. FYI, I did not find a solution to the above problem, so instead of downloading the Google Sheets file and replacing the local Excel file with it, I changed the program to update only needed columns in the local Excel file by fetching them in array format from the corresponding Google Sheets file. – nvkrj Mar 02 '17 at 14:57
  • @Nvj what do you mean with comparing mime types did not matter? If you want to save a Google Sheets file to a local Excel file converting to the right mime type actually does matter. I reproduces your problem, works fine on my side with both Excel 2007 and Excel 2016 when converting to the right mime type. I fetch the Google sheets file as byte array. Then write the bytes to a file with the right mime type. – MetalJacketNL Mar 15 '17 at 14:43
  • Old post, but getting error that Non-invocable member 'File' cannot be used like a method on the return line. – AWizardInDallas Jan 15 '19 at 17:20