简体   繁体   中英

How can I save as word.docx in open xml

I need to open a template, add some text, and then save it with a new name. How can I save it with a new name?

        String Dir = @"F:\template.docx";
     WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(Dir, true);

        Body body = wordprocessingDocument.MainDocumentPart.Document.Body;

        Paragraph paragraph1 = body.AppendChild(new Paragraph());

        Run run1 = new Run();
        RunProperties runProperties1 = new RunProperties();
        Bold bold2 = new Bold();
        FontSize fontSize2 = new FontSize() { Val = "28" };
        runProperties1.Append(bold2);
        runProperties1.Append(fontSize2);

        Text text1 = new Text();
        text1.Text = "text";
        run1.Append(runProperties1);
        run1.Append(text1);
        paragraph1.Append(Hearder1);
        paragraph1.Append(run1);

        wordprocessingDocument.Close();
byte[] byteArray = File.ReadAllBytes("D:\Document.docx");    
using (var stream = new MemoryStream())
    {
        stream.Write(byteArray, 0, (int)byteArray.Length);
        using (var document = WordprocessingDocument.Open(stream, true))
        {
           //Here fill document
        }
        // And save file
        File.WriteAllBytes("D:\NewDocument.docx", stream.ToArray()); 
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM