Create .docx file

Peter_1985 2,586 Reputation points

How to create one .docx file using C sharp?

A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,324 questions
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,604 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 83,021 Reputation points

    You can use Open XML which works fine
    (can be done with Office Interop too, but it needs Office to be installed)

    Basic sample : Create a word processing document by providing a file name

1 additional answer

Sort by: Most helpful
  1. Karen Payne MVP 35,376 Reputation points

    Install the following NuGet package DocumentFormat.OpenXml.

    Try creating a new document

    using (var document = WordprocessingDocument.Create(fileName, WordprocessingDocumentType.Document))
        MainDocumentPart mainPart = document.AddMainDocumentPart();
        mainPart.Document = new Document();
        mainPart.Document.AppendChild(new Body());

    The above is taken from Office Word Basic operations: Open XML SDK 2.5 Office Word documents with source code in the following repository project.

    EDIT: Code sample to create a new document and write a single paragraph.