如何将文档实例转换为文件流

发布于 2024-12-27 20:11:26 字数 1241 浏览 1 评论 0原文

在过去的一个小时里我一直在努力解决这个问题。

有人可以帮助我将 Microsoft.Office.Interop.Word.Document 的实例转换为 FileStream 吗?我下面有这个功能,它不起作用,但可能会帮助你们了解我想要做什么:

 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;
         }
     }
 }

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? 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;
         }
     }
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

辞取 2025-01-03 20:11:26

像这样的事情应该有效:

 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);

         }
 }

并且您将来必须删除临时文件。

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.

燃情 2025-01-03 20:11:26

您必须将文档保存到文件系统,然后将其读入 MemoryStream。我不认为序列化是这里的一个选项,因为 Document 类可能不是可序列化的。

You muse save the document to the file system and then read that into a MemoryStream. I don't think serialization would be an option here because Document class is not Serializable probably.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文