我正在尝试打开 .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# - 在不启动 MSWord 的情况下读取 .doc 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3755100/