如何创建字节数组Microsoft.Office.Interop.Word.Document对象,不将其保存到磁盘? [英] How to create Microsoft.Office.Interop.Word.Document object from byte array, without saving it to disk?

查看:245
本文介绍了如何创建字节数组Microsoft.Office.Interop.Word.Document对象,不将其保存到磁盘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能创建字节数组 Microsoft.Office.Interop.Word.Document 对象,不使用C#它保存到磁盘?



 公共静态INT GetCounterOfCharacter(字节[] wordContent)
{
应用objWord =新的应用程序();
Microsoft.Office.Interop.Word.Document objDoc =新的文件();
// objDoc = objWord.Documents.Open((D:/Test.docx);我想从字节数组wordContent
返回objDoc.Characters.Count创建文件;
}


解决方案

有没有这样做的直接的方式这个据我知道这个词的互操作库无法从字节流中读取除非你有巨大的(或一个巨大的量)文件时,我会建议只需使用一个tmp文件:

 应用程序=新的应用程序(); 

字节[] wordContent = GetBytesInSomeWay();

VAR TMPFILE = Path.GetTempFileName();
VAR tmpFileStream = File.OpenWrite(TMPFILE);
tmpFileStream.Write(wordContent,0,wordContent.Length);
tmpFileStream.Close( );

app.Documents.Open(TMPFILE);

我知道这是不是你要找的答案,但在这样的情况下(这里做你真正想做的事,需要相当多的时间和坐立不安)它可能是值得考虑是否开发时间远远超过运行时性能。



如果您仍然希望寻找到一种方式来解决它,你就打算的方式,我建议你在的这个主题。


How can I create a Microsoft.Office.Interop.Word.Document object from byte array, without saving it to disk using C#?

public static int GetCounterOfCharacter(byte[] wordContent)
{
   Application objWord = new Application();
   Microsoft.Office.Interop.Word.Document objDoc = new Document();
   //objDoc = objWord.Documents.Open(("D:/Test.docx"); i want to create document from byte array "wordContent"
   return  objDoc.Characters.Count;      
}

解决方案

There is no straight-forward way of doing this as far as I know. The Word interop libs are not able to read from a byte stream. Unless you are working with huge (or a huge amount of) files, I would recommend simply using a tmp file:

Application app = new Application();

byte[] wordContent = GetBytesInSomeWay();

var tmpFile = Path.GetTempFileName();
var tmpFileStream = File.OpenWrite(tmpFile);
tmpFileStream.Write(wordContent, 0, wordContent.Length);
tmpFileStream.Close();

app.Documents.Open(tmpFile);

I know this isn't the answer you're looking for, but in a case like this (where doing what you really want to do requires quite a bit of time and fidgeting) it might be worth considering whether or not development time outweighs runtime performance.

If you still want to look into a way to solve it the way you intend it to, I'd recommend the answers in this thread.

这篇关于如何创建字节数组Microsoft.Office.Interop.Word.Document对象,不将其保存到磁盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆