【问题标题】:Microsoft.Office.Interop.Word .doc/.docx lines count C#Microsoft.Office.Interop.Word .doc/.docx 行数 C#
【发布时间】:2022-06-15 20:53:00
【问题描述】:

我要统计word文档(.doc/.docx)的总行数。

在我的课堂上,我添加了对 COM 库 Microsoft.Office.Interop.Word 的引用,我通过它计算文档的总字数。

参考这个Lines.Count Property 文档,该库还在最新版本中提供了行数选项。

但不幸的是,我在整个库中找不到Lines 接口或属性。 有没有其他方法可以得到MS Word文档的总行数,如下图所示?

Click here to view image

字数统计方法(仅供参考)

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;
        }
    }

【问题讨论】:

标签: c# asp.net-core


【解决方案1】:

从之前回答的问题Here 中,可能会对您有所帮助。 This too,可以使用 File.ReadLines 方法。

【讨论】:

  • 嗯,这支持文本和文档文件。在我的情况下 .doc / .docx 文件。
猜你喜欢
  • 1970-01-01
  • 2012-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-10
  • 1970-01-01
相关资源
最近更新 更多