当我尝试使用 DocumentFormat.OpenXml dll 读取 .doc 文件时,出现“文件包含损坏的数据”错误。
此 dll 正在正确读取 .docx 文件。
DocumentFormat.OpenXml dll 可以帮助读取.doc 文件吗?
string path = @"D:\Data\Test.doc";
string searchKeyWord = @"java";
private bool SearchWordIsMatched(string path, string searchKeyWord)
{
try
{
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(path, true))
{
var text = wordDoc.MainDocumentPart.Document.InnerText;
if (text.Contains(searchKeyWord))
return true;
else
return false;
}
}
catch (Exception ex)
{
throw ex;
}
}
最佳答案
旧的 .doc 文件与新的 .docx 文件具有完全不同的格式。所以,不,您不能使用 OpenXml 库来读取 .doc 文件。
为此,您需要先手动转换文件,或者需要使用 Office 互操作,而不是您现在使用的 Open XML SDK。
关于c# - 使用 DocumentFormat.OpenXml dll 读取 .Doc 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9975008/