简体   繁体   English

在C#中使用openxml在现有docx文件中添加html内容

[英]add html content in existing docx file using openxml in C#

How do I add/append HTML content in an existing .docx file, using OpenXML in asp.net C#? 如何在asp.net C#中使用OpenXML在现有.docx文件中添加/附加HTML内容?

In an existing word file, I want to append the html content part. 在现有的Word文件中,我要附加html内容部分。 For example: 例如:

In this example, I want to place "This is a Heading" inside a H1 tag. 在此示例中,我想将“ This is a Heading”放置在H1标签内。

Here its my code 这是我的代码

protected void Button1_Click(object sender, EventArgs e)
    {

        try
        {
            using (WordprocessingDocument doc = WordprocessingDocument.Open(@"C:\Users\admin\Downloads\WordGenerator\WordGenerator\FTANJS.docx", true))
            {
                string altChunkId = "myId";
                MainDocumentPart mainDocPart = doc.MainDocumentPart;

                var run = new Run(new Text("test"));
                var p = new Paragraph(new ParagraphProperties(new Justification() { Val = JustificationValues.Center }), run);

                var body = mainDocPart.Document.Body;
                body.Append(p);


                MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes("<html><head></head><body><h1>HELLO</h1></body></html>"));

                // Uncomment the following line to create an invalid word document.
                // MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes("<h1>HELLO</h1>"));

                // Create alternative format import part.
                AlternativeFormatImportPart formatImportPart =
                   mainDocPart.AddAlternativeFormatImportPart(
                      AlternativeFormatImportPartType.Html, altChunkId);
                //ms.Seek(0, SeekOrigin.Begin);

                // Feed HTML data into format import part (chunk).
                formatImportPart.FeedData(ms);
                AltChunk altChunk = new AltChunk();
                altChunk.Id = altChunkId;

                mainDocPart.Document.Body.Append(altChunk);
            }
        }
        catch (Exception ex)
        {

            ex.ToString ();
        }


    }

The short answer is "You can't add HTML to a docx file". 简短的答案是“您不能将HTML添加到docx文件”。

Docx is an open format defined here . Docx是此处定义开放格式 If you're using the Microsoft version they have a number of extensions. 如果您使用的是Microsoft版本,则它们具有许多扩展名。

In any case, the file contains XML, not HTML and you can't simply add HTML to a docx file. 无论如何,文件都包含XML,而不是HTML,并且您不能简单地将HTML添加到docx文件中。 There are styles and formatting objects and pointers that all need to be updated. 有些样式,格式设置对象和指针都需要更新。

If you need to modify a docx file and don't want to do a lot of research and a lot of coding, you'll need to find an existing library to work with. 如果您需要修改docx文件并且不想进行大量研究和大量编码,则需要找到一个现有的库来使用。

Add HTML content as Chunk should work, and you are almost there. 添加HTML内容,因为Chunk应该可以工作,您几乎可以使用了。

If I understand the question properly, this code should work. 如果我正确理解了该问题,则此代码应该可以工作。

        //insert html content to H1 tag
        using(WordprocessingDocument fDocx = WordprocessingDocument.Open(sDocxFile,true))
        {
            string sChunkID = "myhtmlID";
            AlternativeFormatImportPart oChunk = fDocx.MainDocumentPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.Html, sChunkID);
            using(FileStream fs = File.Open(sHtml,FileMode.OpenOrCreate))
            {
                oChunk.FeedData(fs);
            }
            AltChunk oAltChunk = new AltChunk();
            oAltChunk.Id =sChunkID ;

            //insert html to the tag of 'H1' and remove H1.
            Body body = fDocx.MainDocumentPart.Document.Body;
            Paragraph theParagraph = body.Descendants<Paragraph>().Where(p => p.InnerText == "H1").FirstOrDefault();
            theParagraph.InsertAfterSelf<AltChunk>(oAltChunk);
            theParagraph.Remove();

            fDocx.MainDocumentPart.Document.Save();
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM