如何将文档实例转换为文件流
在过去的一个小时里我一直在努力解决这个问题。
有人可以帮助我将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
像这样的事情应该有效:
并且您将来必须删除临时文件。
Something like this should work:
and you'll have to delete the temp file sometime in the future.
您必须将文档保存到文件系统,然后将其读入 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.