如何将 MS Word 文档中的文本解析为字符串 [英] How to parse text from MS Word document to string

查看:25
本文介绍了如何将 MS Word 文档中的文本解析为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到一种方法将 word 文档的文本解析为我的项目中的字符串.我有 600 多个 word(.doc) 文件需要获取文本内容(如果有新的行和制表符可能)并将其分配给每个字符串.

I am trying to find a way to parse a word document's text to a string in my project.I have more than 600 word(.doc) files that I need to get the text content(with the new lines and tabs if possible) and assign it to a string for each one.

我一直在阅读有关 Open XML SDK 的内容,但对于看起来如此简单的东西来说,它看起来相当复杂.

I've been reading stuff about the Open XML SDK but it looks quite complicated for something that looks so simple.

推荐答案

Open XML SDK 仅适用于 2007 及更新的格式,使用起来并不简单.

Open XML SDK is only for 2007 and newer formats and it is not trivial to use.

如果性能不是问题,您可以使用 Word Automation 并让 Word 为您执行此操作.它看起来像这样:

If performance is not an issue you could use Word Automation and have Word do this for you. It will look something like this:

var app = new Application();
var doc = app.Documents.Open(documentLocation);

string rangeText = doc.Range().Text;

doc.Save();
doc.Close();

Marshal.ReleaseComObject(doc);    
Marshal.ReleaseComObject(app);

看看http://www.codeproject.com/Articles/18703/Word-2007-Automationhttp://www.codeproject.com/Articles/21247/Word-Automation 获取更完整的示例和说明.请注意,如果您的文档移动复杂(脚注、文本框、表格...),这可能会变得更加棘手.

Take a look at http://www.codeproject.com/Articles/18703/Word-2007-Automation or http://www.codeproject.com/Articles/21247/Word-Automation for more complete examples and instructions. Note that this may become a bit more tricky if your documents are move complex (footnotes, text boxes, tables...).

另一种选择是将文档保存为文本,然后读取文本文件.看看这个 - http://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.document.saveas(v=vs.80).aspx

Another option is have word save the document as a text and then read the text file. Take a look at this - http://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.document.saveas(v=vs.80).aspx

这篇关于如何将 MS Word 文档中的文本解析为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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