首页 文章

将Google表格导出到Excel 2016

提问于
浏览
-1

我在.NET框架中创建一个控制台应用程序,将.XLSX文件上传到Google Cloud 端硬盘(在此过程中将其转换为Google表格格式),并将Google表格下载为.XLSX格式 .

基本上我正在尝试将本地驱动器与Google Cloud 端硬盘同步,同时保留工作表的所有格式 .

我已经使用Google Drive for .NET框架执行此操作并且它运行良好,除了将Google表格下载到.XLSX文件时,下载的.XLSX文件在Excel 2007中没有任何问题打开,但显示了使用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?

单击“是”,Excel将成功打开文件,但会显示包含错误日志的消息 . 我打开了错误日志,除了提到“条件格式”之外,其中没有任何细节 . 此外,原始Google表格中的一些冻结行在Excel 2016中不再冻结,但在Excel 2007中打开文件时完好无损 .

您是否有任何建议可以将从Google表格下载的.XLSX文件与Excel 2016兼容,或采用完全不同的方法来消除不同版本和不同格式的问题?

1 回答

  • 0

    由于这是一个旧帖子,你可能已经解决了你的问题 . 但是为了将来参考,我将尝试使用Google Drive api的v3回答您的问题 .

    Google Cloud 端硬盘使用自己的MIME类型 . 可以找到这些MIME类型的列表here .

    Google Drive MIME可以使用相应的MIME类型进行转换 . 可以在this页面的底部找到相应MIME类型的列表 .

    因此,如果您想转换Google,您必须找出它的MIME类型以及相应的MIME类型 .

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

    您可以使用以下方法获取文件的MIME类型:

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

    在切换案例中,我检查MIME类型是否为Google文档MIME类型 . 如果是,则将MIME类型设置为Google文档的相应MIME类型 . 如果它不是Google文档,您可以使用您从文件中获得的MIME类型 . 如果您上传的文件不是Google文档,则Google Cloud 端硬盘不会更改MIME类型 .

    请注意,上面的示例是在ASP.NET MVC项目中使用的 .

相关问题