我想将一个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
但是当我想阅读它时,我不知道如何从数据库中获取的流生成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);