Create .docx file

Peter_1985 2,486 Reputation points
2021-10-06T09:12:27.637+00:00

Hi,
How to create one .docx file using C sharp?

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,292 questions
C#
C#
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,219 questions
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 81,481 Reputation points
    2021-10-06T09:15:45.897+00:00

    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,031 Reputation points
    2021-10-06T14:29:06.743+00:00

    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());
    
        mainPart.Document.Save();
     }
    

    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.