循环目录并追加到文件 [英] Loop through directory and append to file

查看:59
本文介绍了循环目录并追加到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试选择一个路径,然后我想读取目录,在每个目录中都有多个word文件,我想打开每个word文件,复制其内容并将其粘贴到一个新的word文档中例如,如果我在
目录中有6个word文件,我想打开一个新的word文档,然后打开目录中的第一个文档,复制其内容并将其粘贴到新文档中,然后循环直到所有文档已经阅读。

I am trying to select a path , then i want to read the directories , in each directory there are multiple word files , i want to open each word file , copy its contents and paste it in a new word documents , example if i have 6 word files in a directory , i want to open a new word document , then open the first document in the directory , copy its contents and paste it to the new document , and loop untill all documents are read.

到目前为止我可以遍历文档,但新创建的word文件只能获取最后打开的文档..下面是我的代码希望有人可以帮助!!!

by far im able to loop over the docs , but the newly created word file is only getting the last opened document ... below is my code hope someone can help !!!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
using System.IO;

namespace combiner
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {


            // For optional parameters create a missing object

            object missing = System.Reflection.Missing.Value;

            // Create an object of application class

            Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.Application();

            // add a document in the Application

            Document adoc = WordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing);

            // declare variables for setting the position within the document

            object start = 0;

            object end = 0;

            // create a range object which starts at 0

            Range rng = adoc.Range(ref start, ref missing);



            string path = @"C:\demo";


            string[] files = Directory.GetDirectories(path);

            for (int i = 0; i < files.Length; i++)
            {


                string[] ora = Directory.GetFiles(files[i], "*.docx", SearchOption.AllDirectories);

                foreach (string f in ora)
                {

                    MessageBox.Show(f);
                    textBox1.Text = f.ToString();


                    // insert a file

                    rng.InsertFile(textBox1.Text, ref missing, ref missing, ref missing, ref missing);

                    // now make start to point to the end of the content of the first document

                    start = WordApp.ActiveDocument.Content.End - 1;

                    // create another range object with the new value for start

                    Range rng1 = adoc.Range(ref start, ref missing);

                    // insert the another document

                    rng1.InsertFile(textBox1.Text, ref missing, ref missing, ref missing, ref missing);

                    // now make start to point to the end of the content of the first document

                    start = WordApp.ActiveDocument.Content.End - 1;




                }





                // make the word appliction visible

                WordApp.Visible = true;
                adoc.SaveAs2(@"D:\fulltest.docx");
            }


        }
    }
}






GN



GN

推荐答案

你好, 

Hello, 

你描述了一些东西 - 文件夹,文件/文件和等等...

You describe few things - folders, files/documents & etc...

你只有一个功能,试图管理情况。

And you have only one function where trying to manage situation.

你能分开吗?

你能创建一个能正确合并两个文件的函数吗?

Can you create a function which will properly merge two documents?

你能创建一个文件夹内的文件表示和文件导航吗?

Can you create a representation of folder-with-document inside and navigation through documents?

你能让这些零件分开工作吗?并加入一个工作部分到programm? 

Can you make those parts work separately? And join a working part into programm? 

从简单的事情开始:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication1
{
    public class WordDocumentMerge
    {
        public static string Merge(string target, string source)
        {
            Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.Application();
            ...
            return target;
        }
    }
}

让它按要求运作。 

Make it work as required. 

然后 - 看看如何获​​得正确的文件名和电话。

Then - look on how to get proper filenames and a call.


这篇关于循环目录并追加到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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