【发布时间】:2012-07-11 19:49:12
【问题描述】:
我想在数据库中保存一个word文档,我这样做:
FileStream st = new FileStream(filePath, FileMode.Open);
byte[] buffer = new byte[st.Length];
st.Read(buffer, 0, (int)st.Length);
st.Close();
//save Buffer into DB
//myentityobject.FileContent = buffer;
//save it
但是当我想阅读它时,我不知道如何从我从 DB 获得的流中制作一个 word 文档。我尝试过这样的事情:
var doc = myentityobject.FileContent;
MemoryStream stream = new MemoryStream(doc as byte[]);
letterPatternDoc = new Document(stream);
filePath = @"D:\LetterPatternDocument001.doc";
object oMissing = System.Reflection.Missing.Value;
currentApp = new wd.Application();
currentApp.Visible = true;
currentDoc = currentApp.Documents.Add(Type.Missing, Type.Missing);
currentDoc = doc;
currentDoc.SaveAs(ref path, oMissing, oMissing, oMissing, oMissing, oMissing, false
, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
但它不起作用。
编辑:
我更改了代码,它现在可以工作了,我通过文件流保存文件,然后读取它。但我不知道这是一个好的解决方案吗?
FileStream fileSream = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite);
BinaryWriter bw = new BinaryWriter(fileSream);
bw.Write(FileContent);//filecontent is a byte[]
fileSream.Close();
// WordDoc.OpenDocument(filePath);
【问题讨论】:
-
详细说明“不起作用”......它做什么(或不做什么)
-
@ psubsee2003 : 正如我所说,我在数据库中保存了一个 word 文档,现在我想在 word 应用程序中阅读并显示它。
-
什么是 bw.Write(ViewSource.CurrentItem.LetterPatternDocument.FileContent);那是你自己的观众吗?
标签: c# stream office-interop