Export HTML to Word .docx in ASP.NET Core

People want to take a document away as a real Word file: a report for a client or a contract for signature. Many solutions send your document to a paid cloud service. RichTextBox does it on your own server, with the MIT-licensed Open XML SDK, and nothing leaves your application.
It comes with the upload endpoint
app.MapRichTextBoxUploads(); // also maps POST /richtextbox/export/docx
There is no extra package to install and no separate service to run.
From a button on your page
With the AI toolkit turned on for the editor, a helper posts the HTML and starts the download:
<richtextbox id="DocxEditor" name="Body" toolbar="default" height="460px" enable-ai-toolkit="true"></richtextbox> <button type="button" id="export">Export to .docx</button>
document.getElementById("export").addEventListener("click", function () {
var editor = window.richTextBoxEditors.DocxEditor;
editor.aiToolkit.exportDocx({
fileName: "quarterly-update.docx",
title: "Quarterly update",
onError: function (err) { console.error(err); }
});
});
Without any JavaScript helper
The endpoint is a plain POST, so anything can call it. This is a quick way to test it too:
curl -X POST "https://your-site/richtextbox/export/docx" \
-H "Content-Type: application/json" \
-d '{ "html": "<p>Hello</p>", "title": "Memo" }' \
-o memo.docx
Or the toolbar button
Name exportdocx in your toolbar for a button, with importdocument for bringing a file in and exportpdf for a PDF:
<richtextbox name="Body" toolbar="{bold,italic}|{importdocument,exportdocx,exportpdf}|{undo,redo}"></richtextbox>
Use the endpoint when your application has to produce the file itself, such as from a scheduled job or an API.
What comes across
Headings, bold and italic, lists, links and tables become real Word structure, so people can keep editing them, rather than a picture of the page. As with any conversion, test with your own documents, especially anything that relies on your website's CSS, because Word does not read your stylesheet.
Protect it
The endpoint is in the same group as uploads, so your authorization policy covers it.