c# – 在不启动MSWord的情况下读取.doc文件
作者:互联网
我正在尝试打开.doc文件并阅读其内容.但是,如果不启动MSWord,我无法找到任何方法.
现在我有以下代码:
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
object nullObject = System.Reflection.Missing.Value;
object file = @"C:\doc.doc";
Microsoft.Office.Interop.Word.Document doc = app.Documents.Open(ref file, ref nullObject, ref nullObject,
ref nullObject, ref nullObject, ref nullObject, ref nullObject, ref nullObject, ref nullObject,
ref nullObject, ref nullObject, ref nullObject, ref nullObject, ref nullObject, ref nullObject,
ref nullObject);
doc.ActiveWindow.Selection.WholeStory();
doc.ActiveWindow.Selection.Copy();
IDataObject data = Clipboard.GetDataObject();
string text = data.GetData(DataFormats.Text).ToString();
doc.Close(ref nullObject, ref nullObject, ref nullObject);
app.Quit(ref nullObject, ref nullObject, ref nullObject);
但是它推出了MSWord,任何解决方案都可以在不启动的情况下实现吗?
解决方法:
两种可能性:使用Microsoft’s spec为.doc格式编写自己的解析器,或者为此目的使用现有库(例如,从Aspose开始).除非你有几年的时间花在这项任务上,否则后者显然是正确的选择.
标签:c,net,ms-office,doc-2 来源: https://codeday.me/bug/20190626/1298754.html