如何阅读谷歌文档的内容

时间:2017-06-02 13:20:16

标签: c# google-drive-api

我使用了https://developers.google.com/drive/v3/web/manage-downloads,并且我有一份代码的工作副本,可以从Google驱动器下载具有权限访问权限的文件。

我有什么方法可以阅读word文档(使用gdrive-New- New Doc创建)

下载文件的示例代码:

private static void DownloadFile(string fileId, DriveService driveService)
{

    var request = driveService.Files.Export(fileId, "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
    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 +=
            (IDownloadProgress progress) =>
            {
                switch (progress.Status)
                {
                    case DownloadStatus.Downloading:
                        {
                            Console.WriteLine(progress.BytesDownloaded);
                            break;
                        }
                    case DownloadStatus.Completed:
                        {
                            Console.WriteLine("Download complete.");
                            break;
                        }
                    case DownloadStatus.Failed:
                        {
                            Console.WriteLine("Download failed.");
                            break;
                        }
                }
            };


    request.Download(stream);

    using (FileStream file = new FileStream("file.doc", FileMode.Create, FileAccess.Write))
    {
        stream.WriteTo(file);
    }
}

2 个答案:

答案 0 :(得分:0)

如果它是真正的DOC文件,那么您可以使用

获取文本

Code7248.word_reader

private string readFileContent(string pDocPath)
{
   if(!System.IO.File.Exists(pDocPath))
   {
       Console.WriteLine("The file doesn't exist");
       return String.Empty;
   }

   var extractor = new TextExtractor(pDocPath);
   var txt = extractor.ExtractText();
   Console.WriteLine(txt);
   return txt;
}

static void Main(string[] args)
{       
   var strDocPath = @"C:\Temp\yourWordDoc.doc";
   var strDocTxt = readFileContent(strDocPath);
}

答案 1 :(得分:0)

您可能需要在Google云端硬盘用户界面中检查open files的这两种方式:

  

使用上下文菜单项打开

  • 调用files.get检查用户对文档的权限,获取元数据并下载文件内容。如果使用Google文档MIME类型创建文件,请打开Goog​​le文档并将其转换为支持的格式。
  

用于打开文件的Google Picker对话框

  • 应用程序可以显示文件选择器以从应用程序本身中选择内容。有关其他详细信息,请参阅files picker guide
相关问题