question

Jackson1990-7147 avatar image
0 Votes"
Jackson1990-7147 asked karenpayneoregon commented

Create .docx file

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

dotnet-csharpdotnet-xamarinforms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Castorix31 avatar image
0 Votes"
Castorix31 answered Jackson1990-7147 commented

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


· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

What is reason of error

System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=WindowsFormsApp1

due to last line (with body line)?

      static void Main()
      {
          WriteToNewWordDoc("y:/d2.docx", "Oct-6 One more line");
          Application.EnableVisualStyles();
          Application.SetCompatibleTextRenderingDefault(false);
          Application.Run(new Form1());
      }
      public static void WriteToNewWordDoc(string filepath, string txt)
      {
          // Open a WordprocessingDocument for editing using the filepath.
          //using (WordprocessingDocument wordprocessingDocument =
          //     WordprocessingDocument.Open(filepath, true))
          //{
          using (WordprocessingDocument wordDocument =
  WordprocessingDocument.Create(filepath, WordprocessingDocumentType.Document))
          {
          // Assign a reference to the existing document body.
          Body body = wordDocument.MainDocumentPart.Document.Body;


0 Votes 0 ·
Castorix31 avatar image Castorix31 Jackson1990-7147 ·

You create a document, so there is no Body yet
See the code for the creation of a new document : Sample Code


0 Votes 0 ·

you just have to copy-paste MSDN code, and the .docx is created

0 Votes 0 ·
karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered karenpayneoregon commented

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.


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

This is copy-paste of MSDN code already posted... useless

0 Votes 0 ·

Not a copy and paste.

0 Votes 0 ·