简体   繁体   English

如何将文档实例转换为FileStream

[英]How to convert a Document Instance to a FileStream

I've Been struggling with this in the past hour. 在过去的一小时里,我一直在努力解决这个问题。

Can someone help me to convert an instance of Microsoft.Office.Interop.Word.Document to FileStream? 有人可以帮我将Microsoft.Office.Interop.Word.Document的实例转换为FileStream吗? I have this function below which does not work but might help you guys to have an idea of what im trying to do: 我有下面的这个功能不起作用,但可能会帮助你们了解我想做什么:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Runtime.Serialization;
 using System.ServiceModel;
 using System.Text;
 using Microsoft.Office.Interop.Word;
 using System.IO;

 namespace DropDownTemplate.Web.WebServices.Word
 {
     // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "WordGenerator" in code, svc and config file together.
     public class WordGenerator : IDocumentGenerator
     {
         FileStream IDocumentGenerator.GenerateDocument()
         {
             // For optional parameters create a missing object
             object missing = System.Reflection.Missing.Value;
             // open the document specified in the fileName variable
             Document adoc = new Document();
             adoc.InlineShapes.AddPicture(@"http://localhost:2014/Resources/MG.PNG", ref missing, ref missing, ref missing);
             using (StreamReader y = new StreamReader())
             {
                 y.Read(adoc);   
             }
             return adoc;
         }
     }
 }

You muse save the document to the file system and then read that into a MemoryStream. 您可以将文档保存到文件系统,然后将其读入MemoryStream。 I don't think serialization would be an option here because Document class is not Serializable probably. 我认为序列化不是一个选项,因为Document类可能不是Serializable。

Something like this should work: 这样的事情应该有效:

 public class WordGenerator : IDocumentGenerator
 {
         FileStream IDocumentGenerator.GenerateDocument()
         {
                object missing = System.Reflection.Missing.Value;
                Document adoc = new Document();
                adoc.InlineShapes.AddPicture(@"http://localhost:2014/Resources/MG.PNG", ref missing, ref missing, ref missing);

                // save the doc file
                object fileName = Path.GetTempFileName();
                adoc.SaveAs(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

                // quit word and release COM objects
                object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
                adoc.Application.Quit(ref saveChanges, ref missing, ref missing);
                Marshal.ReleaseComObject(adoc);

                // return the stream
                return new FileStream((string)fileName, FileMode.Open);

         }
 }

and you'll have to delete the temp file sometime in the future. 并且您将来必须删除临时文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM