在C#中使用openxml在现有docx文件中添加html内容 [英] add html content in existing docx file using openxml in C#

查看:176
本文介绍了在C#中使用openxml在现有docx文件中添加html内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在asp.net C#中使用OpenXML在现有的.docx文件中添加/添加HTML内容?

How do I add/append HTML content in an existing .docx file, using OpenXML in asp.net C#?

在现有的Word文件中,我想附加html内容部分. 例如:

In an existing word file, I want to append the html content part. For example:

在此示例中,我想将"This is a Heading"放置在H1标记内.

In this example, I want to place "This is a Heading" inside a H1 tag.

这是我的代码

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 ();
        }


    }

推荐答案

添加HTML内容,因为Chunk应该可以工作,而且您已经差不多了.

Add HTML content as Chunk should work, and you are almost there.

如果我对问题的理解正确,那么此代码应该可以正常工作.

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();
        }

这篇关于在C#中使用openxml在现有docx文件中添加html内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆