如何从字节数组加载Word文档

我将整个MS Word文件本身保存到一个字节数组中。想要按照我在文件系统上的方式加载它,但是使用最少的Microsoft.Office.Interop.Word,因为它很慢.Open(args[])部分。

试试这个….

  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 

没有支持的方法可以使用Interop.Word直接进行 ,因为没有支持字节数组的方法。

作为一种可行的解决方法,您可以通过以下方式使用临时文件:

 // 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); 

有关其他信息, 请在我的博客上阅读此帖 。

方法public static byte[] ReadAllBytes( string path )将所有文件信息返回到一个字节数组中。 您不必担心流,因为MSDN文档说: “给定文件路径,此方法打开文件,将文件的内容读入字节数组,然后关闭文件。”

如果您想了解更多信息,请查看此链接