我正在尝试获取事件 Word 文档的 base64 表示,同时它仍在 Word 中打开 并在 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
最佳答案
您是正确的 - Word 锁定了当前文档。为了获取当前文档字节,您需要复制现有文件 ( File.Copy
) 或保存到新文件 ( Document.SaveAs
或 IPersistFile.Save
)阅读其内容。
关于c# - 将Word文档转换为base64字符串C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11562644/