How to load Word document from byte array

。_饼干妹妹 提交于 2019-12-04 06:08:23

Try this....

      byte[] bte = File.ReadAllBytes("E:\\test.doc"); // Put the Reading file
        File.WriteAllBytes(@"E:\\test1.doc", bte); // Same contents you will get in byte[] and that will be save here 

There is no supported way to do it right off-the-bat using Interop.Word, as there are no methods supporting byte arrays.

As a viable workaround you can use a temporary file in the following way:

// byte[] fileBytes = getFileBytesFromDB();
var tmpFile = Path.GetTempFileName();
File.WriteAllBytes(tmpFile, fileBytes);

Application app = new word.Application();
Document doc = app.Documents.Open(filePath);

// .. do your stuff here ...

doc.Close();
app.Quit();
byte[] newFileBytes = File.ReadAllBytes(tmpFile);
File.Delete(tmpFile);

Fore additional info, read this post on my blog.

The method public static byte[] ReadAllBytes( string path ) returns all the file information into a byte array. You dont have to worry about the stream, as the MSDN documentation says: "Given a file path, this method opens the file, reads the contents of the file into a byte array, and then closes the file."

Check out this link if you want more information

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!