c# Microsoft.Office.Interop.Word.Document to Stream

本文关键字:Document to Stream Word Interop Microsoft Office | 更新日期: 2023-09-27 18:09:18

我有一个活动文档:

Microsoft.Office.Interop.Word.Document document = Globals.ThisAddIn.Application.ActiveDocument;

如何将其转换为流?

或者更好的是,如何将活动文档发送到rest web服务?

谢谢

c# Microsoft.Office.Interop.Word.Document to Stream

哪个rest-service?如果它是SharePoint或类似的,我习惯只是调用savea函数如下:

Microsoft.Office.Interop.Word.Document document = Globals.ThisAddIn.Application.ActiveDocument;
document.SaveAs("https://www.contoso.sharepoint.com/Documents/Document1.docx");

编辑:P.S.你可以序列化任何流。在这里找到了答案

public static MemoryStream SerializeToStream(object o)
{
    MemoryStream stream = new MemoryStream();
    IFormatter formatter = new BinaryFormatter();
    formatter.Serialize(stream, o);
    return stream;
}
public static object DeserializeFromStream(MemoryStream stream)
{
    IFormatter formatter = new BinaryFormatter();
    stream.Seek(0, SeekOrigin.Begin);
    object o = formatter.Deserialize(stream);
    return o;
}

保存到临时文件,然后根据其他服务的要求读取文件内容,例如字节数组。如果不需要旧的Word格式,则使用Open XML SDK。