我想在数据库中保存一个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);