C# convert HTML to Word

In C#, if you want to create a Word document you can use the OpenXML SDK from Microsoft. With that library you have full control of what you do, however, if you are new to the library, you may not like it. To generate a simple document you may want something more practical, like the HTML to OpenXML library.

I created an example Visual Studio 2012 Project (OpenXML SDK 2.5 dependency downloaded automatically via NuGet) in ASP.NET 4.0 that demonstrates how easy it is, thanks to this library, to generate a document. You can save a file on the server or stream it to the user. My example includes both cases.

Download WordGenerator.zip

Enjoy.

9 thoughts on “C# convert HTML to Word

  1. Paul Brewer

    Hi, Paolo. I modified your WordGenerator example solution to include a canvas element in the Template.html and added some script to convert the canvas to a base64 dataURI image. I also manually added a base64 inline image (cat picture) to the html. When I click the Save button it converts the html template to Word and includes the cat base64 image but not the canvas image. I would be happy to send you the modified VS2017 solution for you to look at.

    Reply
      1. Paul Brewer

        I figured out what the problem is. WordGenerator reads in the original html file. That is, it does not use the modified DOM, so the inline image that replaces the canvas element, using javascript, is not included in the generated Word doc. I’ve got to figure out how to get the modified DOM.

        Reply
        1. Paolo Brocco Post author

          Hi Paul, thanks for the comments, sorry for not getting back to you, I actually forgot to check my blog for a while… if you wanna share something at best make a github repo, I think one day or another I’ll migrate my examples or share my future snippets there, it makes life much easier I mean for other developers to fork, submit pull requests etc. and it’s a bit more accessible for everybody, cheers and thanks again

          Reply
  2. Marouane

    Hi, first of all i would like to thank you for this usefull project,

    However, i have a problem when i’m trying to rotate a text in a table cell, i used CSS property to rotate the content but in the word document the text doesn’t rotate.

    Can you help me to solve this issu please ?

    Reply
    1. Paolo Brocco Post author

      Hi, I’m sorry but I have no time to give support, also last time I used this library it was years ago, sorry. I think you could try asking in stackoverflow. Good luck.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *