Generating docx file from HTML file using OpenXML

后端 未结 3 402
别那么骄傲
别那么骄傲 2020-12-18 06:24

I\'m using this method for generating docx file:

public static void CreateDocument(string documentFileName, string text)
{
    using (Wordproces         


        
3条回答
  •  情话喂你
    2020-12-18 06:48

    You cannot just insert the HTML content into a "document.xml", this part expects only a WordprocessingML content so you'll have to convert that HTML into WordprocessingML, see this.

    Another thing that you could use is altChunk element, with it you would be able to place a HTML file inside your DOCX file and then reference that HTML content on some specific place inside your document, see this.

    Last as an alternative, with GemBox.Document library you could accomplish exactly what you want, see the following:

    public static void CreateDocument(string documentFileName, string text)
    {
        DocumentModel document = new DocumentModel();
        document.Content.LoadText(text, LoadOptions.HtmlDefault);
        document.Save(documentFileName);
    }
    

    Or you could actually straightforwardly convert a HTML content into a DOCX file:

    public static void Convert(string documentFileName, string htmlText)
    {
        HtmlLoadOptions options = LoadOptions.HtmlDefault;
        using (var htmlStream = new MemoryStream(options.Encoding.GetBytes(htmlText)))
            DocumentModel.Load(htmlStream, options)
                         .Save(documentFileName);
    }
    

提交回复
热议问题