【发布时间】:2022-06-15 20:53:00
【问题描述】:
我要统计word文档(.doc/.docx)的总行数。
在我的课堂上,我添加了对 COM 库 Microsoft.Office.Interop.Word 的引用,我通过它计算文档的总字数。
参考这个Lines.Count Property 文档,该库还在最新版本中提供了行数选项。
但不幸的是,我在整个库中找不到Lines 接口或属性。
有没有其他方法可以得到MS Word文档的总行数,如下图所示?
字数统计方法(仅供参考)
public int GetWordsCountFromWordFile(string wordFile)
{
try
{
if (!string.IsNullOrEmpty(wordFile))
{
var application = new Application();
var document = application.Documents.Open(wordFile, ReadOnly: true);
int count = document.Words.Count;
document.Close();
return count;
}
return 0;
}
catch (Exception ex)
{
LogWriter.ErrorLogWriter(nameof(Client), nameof(TaskHelper), nameof(GetWordsCountFromWordFile), "int", ex.Message);
return 0;
}
}
【问题讨论】:
-
您可以使用
built in properties,并获取wdPropertyLines来获取行数。
标签: c# asp.net-core