如何将Word文档保存在Directory.CreateDirectory(docfile_path)创建的文件夹中?

问题描述 投票:0回答:1

此代码块引发一个错误,称为文件名无效。

我想在其中创建一个名为“ test”的文件夹,将有另一个名为今天的日期“ date”的文件夹,我想将word文档保留在该日期文件夹中,请提供帮助。

public string File_path;
public string docfile_path;
public string filename;

private void button1_Click(object sender, EventArgs e)
 {

              string time = DateTime.Now.ToString("HH.mm.ss");
              string date = DateTime.Today.ToShortDateString();

               docfile_path = File_path+ "test" + date;
               Directory.CreateDirectory(docfile_path);

                 filename = docfile_path + "worddoc"+"-" +".docx";


    Word.Application app = new Word.Application();
                Word.Document doc = new Word.Document();
                try
                {
                    doc = app.Documents.Open(filename);
                }
                catch
                {

                }

                Word.Paragraph oPara1;
                oPara1 = doc.Content.Paragraphs.Add();
                oPara1.Range.Text = "Test Result";
                oPara1.Range.Font.Bold = 1;
                oPara1.Format.SpaceAfter = 24;
                oPara1.Range.InsertParagraphAfter();
                oPara1.Range.InsertParagraphAfter();
                 Word.Paragraph oPara2;
                oPara2 = doc.Content.Paragraphs.Add();
                oPara2.Range.Text = "Test Name";
                oPara2.Range.Font.Bold = 1;
                oPara2.Format.SpaceAfter = 24;
                oPara2.Range.InsertParagraphAfter();

                doc.SaveAs2(filename);
                doc.Close();
                doc = null;

                app.Quit();
                app = null;

    }
c# c#-4.0 office-interop com-interop
1个回答
0
投票

令人惊讶的是,此代码编译并运行,但是结果不是您可能想要的。

此代码中有几处错误:

您不能添加类似的字符串来创建路径,应该在目录之间使用'/'符号创建路径。这是一条合法路径:

string path = @"C:\Users\username\Desktop\Games";

这不是:

string path = @"C:UsersusernameDesktopGames";

您可以通过使用Path.Combine函数来修复它,如下所示:

docfile_path = Path.Combine(File_path , "test" , date);

请确保所有路径字符串都正确(包括上面代码中未显示为value的File_path)。

2。您应该使用

Document doc = app.Documents.Add();

而不是创建新的Word文档

Document doc = new Document();

3。对于字符串日期,您应该使用其他格式,DateTime.ToShortDateString()将日期除以'/'符号,这将创建新文件夹。尝试使用:

string date = DateTime.Today.ToString("dd.MM.yyyy");

4。我看不到任何理由

doc = app.Documents.Open(filename);

您正在尝试打开要创建的文件吗?

这是我使用的代码:

        string File_path = @"C:\Users\yakir\Desktop"; 
        string docfile_path;
        string filename;

        string time = DateTime.Now.ToString("HH.mm.ss");
        string date = DateTime.Today.ToString("dd.MM.yyyy");

        docfile_path = Path.Combine(File_path , "test" , date);
        Directory.CreateDirectory(docfile_path);

        filename = Path.Combine(docfile_path, "worddoc" + "-" + ".docx");


        Application app = new Application();
        Document doc = app.Documents.Add();

        Paragraph oPara1;
        oPara1 = doc.Content.Paragraphs.Add();
        oPara1.Range.Text = "Test Result";
        oPara1.Range.Font.Bold = 1;
        oPara1.Format.SpaceAfter = 24;
        oPara1.Range.InsertParagraphAfter();
        oPara1.Range.InsertParagraphAfter();
        Paragraph oPara2;
        oPara2 = doc.Content.Paragraphs.Add();
        oPara2.Range.Text = "Test Name";
        oPara2.Range.Font.Bold = 1;
        oPara2.Format.SpaceAfter = 24;
        oPara2.Range.InsertParagraphAfter();

        doc.SaveAs2(filename);
        doc.Close();
        doc = null;

        app.Quit();
        app = null;
    }
© www.soinside.com 2019 - 2024. All rights reserved.