将用户选择的文档合并为一个的问题

时间:2016-03-19 15:45:00

标签: c#

目前通过“openFileInput'”选择单个文件存在问题。想法是用户使用openfile在1-5个文档之间进行选择,然后根据用户输出文件夹选择将组合按钮合并到一个文档中。

private string[] selectedDocs;

    // input file 1
    string selectedFile1 = @"";
    private void browseFileButton1_Click(object sender, EventArgs e)
    {
        OpenFileDialog openFileInput1 = new OpenFileDialog();
        openFileInput1.Filter = "Word Documents|*.docx;*.doc";
        openFileInput1.Title = "Select a Word Document";

        // Default file, altered when the user selects file of choice 
        openFileInput1.FileName = selectedFile1;

        // initial file path display
        filePath1.Text = openFileInput1.FileName;

        // 'OK' button being confirmed on the popup menu
        if (openFileInput1.ShowDialog() == DialogResult.OK)
        {
            selectedFile1 = openFileInput1.FileName;
            filePath1.Text = openFileInput1.FileName;
        }
    }

    // ** INPUT FILE PROCESS AS SHOWN ABOVE REPEATED 4 MORE TIMES
    // this allows user to insert 4 more files **


    // Output Destination - for separate files
    private string outputFolder2 = @"";
    private void browseButtonOut2_Click(object sender, EventArgs e)
    {
        FolderBrowserDialog diagBrowserOutput2 = new FolderBrowserDialog();
        diagBrowserOutput2.Description = "Select a folder location to save the document...";

        // Default folder, altered when the user selects folder of choice 
        diagBrowserOutput2.SelectedPath = outputFolder2;

        // output file path display
        outputPath2.Text = diagBrowserOutput2.SelectedPath;

        if (DialogResult.OK == diagBrowserOutput2.ShowDialog())
        {
            outputFolder2 = diagBrowserOutput2.SelectedPath;
            outputPath2.Text = diagBrowserOutput2.SelectedPath;
        }
    }

    // combine files in folder selected using MsWord.cs, 
    private void combineButton2_Click(object sender, EventArgs e)
    {
        string mixedFolder = Path.Combine(selectedFile1, selectedFile2, selectedFile3, selectedFile4, selectedFile5);
        selectedDocs = Directory.GetFiles(mixedFolder, "*.doc");
        string outcomeFolder2 = outputFolder2;
        string outputFile2 = "Combined-files.docx";
        string outputFileName2 = Path.Combine(outcomeFolder2, outputFile2);

        MsWord.Merge(selectedDocs, outputFileName2, true);
    }

如果用户选择包含文档列表的文件夹,我目前正在使用此功能,但我尝试启用此选项以使用单个文件选择而不是文件夹选择。这个问题似乎在这里被标记出来了,

selectedDocs = Directory.GetFiles(mixedFolder, "*.doc");

欢迎任何建议或意见 - 谢谢。

1 个答案:

答案 0 :(得分:0)

经过一些实验,我终于让程序工作了。

private void combineButton2_Click(object sender, EventArgs e)
    {
        selectedDocs = new[] 
        {
            selectedFile1,
            selectedFile2,
            selectedFile3,
            selectedFile4,
            selectedFile5
        };
        string outcomeFolder2 = outputFolder2;
        string outputFile2 = "Combined-files.docx";
        string outputFileName2 = Path.Combine(outcomeFolder2, outputFile2);

        MsWord.Merge(selectedDocs, outputFileName2, true);
    }

而不是使用path.combine组合文件,而是使用用户选择的结果文件创建一个数组,然后使用MsWord.cs合并它们