【问题标题】:Convert Word document to base64 string C#将 Word 文档转换为 base64 字符串 C#
【发布时间】:2012-07-19 14:07:35
【问题描述】:

我正在尝试获取仍在 Word 中打开的活动 Word 文档的 base64 表示 并在 ReadAllBytes() 中收到以下错误:

进程无法访问它正在被另一个进程使用的文件“文件路径”

public string GetEncodedTemplate()
        {
            //Convert a Word document's base64 representation
            string base64 = String.Empty;
            _application.ActiveDocument.Save();

            string docPath = _application.ActiveDocument.FullName;
            byte[] binarydata = File.ReadAllBytes(docPath);
            base64 = System.Convert.ToBase64String(binarydata, 0, binarydata.Length);
            return base64;
        }

我确实了解发生错误是因为指定的文档仍在 Word 中打开, 我的问题是 - 是否仍然有可能在不求助于保存到临时文件的情况下获得文档的 base64 表示?

我正在使用 C# .NET 4.0 和 MS Office 2010

【问题讨论】:

    标签: c# .net-4.0 ms-word vsto


    【解决方案1】:

    您是正确的 - Word 已锁定当前文档。为了获取当前文档字节,您需要复制现有文件 (File.Copy) 或保存到新文件(Document.SaveAs 或IPersistFile.Save)用于阅读其内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-12
      • 1970-01-01
      • 1970-01-01
      • 2011-08-31
      • 1970-01-01
      • 1970-01-01
      • 2014-09-14
      相关资源
      最近更新 更多