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

时间:2013-08-12 10:22:05

标签: c# ms-word extract office-interop

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

3 个答案:

答案 0 :(得分:3)

试试这个......

      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 

答案 1 :(得分:2)

没有支持的方法可以使用 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);

我的博客上的其他信息read this post

答案 2 :(得分:-1)

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

如果您想了解更多信息,请查看this link