【问题标题】:C#: How to open multiple word documents from a folderC#:如何从一个文件夹中打开多个word文档
【发布时间】:2017-12-03 15:07:26
【问题描述】:

我有一个文件夹,其中包含三个名为 Test1、Test2 和 Test3 的 word 文档,所有文档都带有 docx 扩展名。

我知道如果我们想手动打开一个文件,我们可以指定一个文件路径并使用以下代码:

Word.Application fileOpen = new Word.Application();
Word.Document document = fileOpen.Documents.Open(filePath);

但是,除了一次打开一个文件之外,还有其他方法可以选择该文件夹吗?我想做以下事情:

  1. 打开 Test1.docx -> 进行一些更改 -> 保存 -> 关闭
  2. 打开 Test2.docx -> 重复上述步骤,最后对 Test3.docx 执行相同操作

我到处搜索,但找不到与此相关的内容。任何帮助将不胜感激,谢谢。

【问题讨论】:

    标签: c# visual-studio ms-word automation office-interop


    【解决方案1】:

    对于这种情况,假设您已经知道要从哪个文件夹打开和读取文件。

    首先,您必须导入 System.IO

    using System.IO;
    

    在该命名空间内,Directory.GetFiles() 将为您提供字符串数组中的文件名。

    private static void FolderReader()
    {
        string folderPath = @"C:\someFolder\";
        Word.Application fileOpen = new Word.Application();
        string[] filePaths = Directory.GetFiles(folderPath);
    
        foreach (string filePath in filePaths)
        {
            Word.Document document = fileOpen.Documents.Open(filePath);
            // perform continue with your algorithm...
            // close the file when you're done.
        }
    }
    

    希望这足以让您入门。祝你好运。

    【讨论】:

      【解决方案2】:

      我找到了我的问题的答案。我在下面包含了示例代码和说明。

      您必须为此使用以下内容!

      Using System.IO;
      

      在本例中,我们将从桌面目录获取所有“doc”和“docx”文件。

      //Directory to search through:
      string sampleFilePath = @"C:\Users\YourUsername\Desktop";
      
      //Now we create a list to hold all the filepaths that exist in this directory
      List<string> allFilePaths = new List<string>();
      //Extract all doc/docx files on Desktop
      string[] start = Directory.GetFiles(sampleFilePath, "*doc");
              for (int begin = 0; begin < start.Length; begin++)
              {
                  allFilePaths.Add(start[begin]);
              }
      
      //The above code does not extract doc/docx files that are contained inside folders that exist on desktop.
      //So we need to get those filepaths separately.
      string[] filePaths = Directory.GetDirectories(sampleFilePath);
              for (int i = 0; i < filePaths.Length; i++)
              {
                  //Any doc and docx files found in the subdirectories are added to the list
                  string[] files = Directory.GetFiles(filePaths[i], "*doc");
                  for (int j = 0; j < files.Length; j++)
                  {
                      allFilePaths.Add(files[j]);
                  }
                  //Continue checking the subdirectories for more folders until you reach the end, then move on to the second subdirectory from the very beginning.
                  string[] checkMoreDirectories = Directory.GetDirectories(filePaths[i]);
                  checkForMoreDirectories(checkMoreDirectories, ref allFilePaths);
              }
      

      下面给出了 checkForMoreDirectories 方法。

      static void checkForMoreDirectories (string[] checkMoreDirectories, ref List<string> myList)
          {
              //This function calls itself recursively for every subdirectory within the previous subdirectory until it reaches the end where there are no more folders left
              //Continuously add any doc/docx files found before going deeper into the subdirectory
              for (int i = 0; i < checkMoreDirectories.Length; i++)
              {
                  string[] files = Directory.GetFiles(checkMoreDirectories[i]);
                  for (int j = 0; j < files.Length; j++)
                  {
                      myList.Add(files[j]);
                  }
                  string[] temp = Directory.GetDirectories(checkMoreDirectories[i]);
                  //Recursive call
                  checkForMoreDirectories(temp, ref myList);
              }
          }
      

      现在,即使您有 1000 个文件夹,每个文件夹都有许多存储 doc/docx 文件的子文件夹,您也将拥有每个存储在列表中的文件路径。然后您可以访问此列表并通过使用 Microsoft Word Interops 打开每个文件并进行更改等。

      【讨论】:

      • Hé @Dylan,当他们也根据您的 cmets 提供答案时,另一个答案的 OP 期望得到不同的结果。如果你有这种感觉,你介意加入meta吗?我们不咬人……
      • 这里有几点要提一下。 1. 我确实对他的回答投了赞成票,但是它说:“记录了少于 15 名声望的投票,但不公开更改显示”。 2. 是的,他的回答是正确的,我按照我在 1 中提到的那样做了。 3. 我发布的代码与他的明显不同,它适用于彼此之间存在的更大规模的子文件夹。
      • 是的,元帖子的评论者确实注意到了声誉要求,我在元答案中明确表示了这一点。从我认为让其他回答者感到惊讶的问题中,您需要一个文件夹的解决方案并不清楚。 AFAICT我们现在都很好。感谢您的评论,祝您度过愉快的一天。
      • @Dylan 你抄袭了我的回答。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-14
      • 1970-01-01
      • 2018-12-11
      • 2012-12-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多