簡體   English   中英

在C#中使用openxml在現有docx文件中添加html內容

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

如何在asp.net C#中使用OpenXML在現有.docx文件中添加/附加HTML內容?

在現有的Word文件中,我要附加html內容部分。 例如:

在此示例中,我想將“ This is a Heading”放置在H1標簽內。

這是我的代碼

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添加到docx文件”。

Docx是此處定義開放格式 如果您使用的是Microsoft版本,則它們具有許多擴展名。

無論如何,文件都包含XML,而不是HTML,並且您不能簡單地將HTML添加到docx文件中。 有些樣式,格式設置對象和指針都需要更新。

如果您需要修改docx文件並且不想進行大量研究和大量編碼,則需要找到一個現有的庫來使用。

添加HTML內容,因為Chunk應該可以工作,您幾乎可以使用了。

如果我正確理解了該問題,則此代碼應該可以工作。

        //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