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

C# 使用C中的openxml在现有的docx文件中添加html内容#,c#,asp.net,openxml,C#,Asp.net,Openxml,如何在asp.net C#中使用OpenXML在现有的.docx文件中添加/追加HTML内容 在现有word文件中,我想附加html内容部分。 例如: 在本例中,我想将“thisaheading”放在H1标记中 这是我的密码 protected void Button1_Click(object sender, EventArgs e) { try { using (WordprocessingDocument doc = Wor

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

在现有word文件中,我想附加html内容部分。 例如:

在本例中,我想将“thisaheading”放在
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 ();
        }


    }
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
尝试
{
使用(WordprocessingDocument doc=WordprocessingDocument.Open(@“C:\Users\admin\Downloads\WordGenerator\FTANJS.docx”,true))
{
字符串altChunkId=“myId”;
MainDocumentPart mainDocPart=doc.MainDocumentPart;
变量运行=新运行(新文本(“测试”));
var p=新段落(新段落属性(新对齐(){Val=justicationvalues.Center}),运行);
var body=mainDocPart.Document.body;
附体(p);
MemoryStream ms=新的MemoryStream(Encoding.UTF8.GetBytes(“HELLO”));
//取消对以下行的注释以创建无效的word文档。
//MemoryStream ms=新的MemoryStream(Encoding.UTF8.GetBytes(“HELLO”));
//创建替代格式导入部件。
可选格式导入部分格式导入部分=
mainDocPart.AddAlternativeFormatImportPart(
AlternativeFormatImportPartType.Html,altChunkId);
//Seek女士(0,SeekOrigin.Begin);
//将HTML数据馈送到格式导入部分(块)。
formatImportPart.FeedData(毫秒);
AltChunk AltChunk=新的AltChunk();
altChunk.Id=altChunkId;
mainDocPart.Document.Body.Append(altChunk);
}
}
捕获(例外情况除外)
{
例如ToString();
}
}
简短的回答是“您不能将HTML添加到docx文件”

Docx是一个很好的工具。如果您使用的是Microsoft版本,它们有许多扩展

在任何情况下,该文件都包含XML,而不是HTML,并且您不能简单地将HTML添加到docx文件中。有一些样式、格式化对象和指针都需要更新


如果您需要修改docx文件,并且不想做大量的研究和编码,那么您需要找到一个现有的库来使用。

添加HTML内容,因为块应该可以工作,您就快到了

如果我正确理解了这个问题,这个代码应该可以工作

        //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();
        }
//将html内容插入H1标记
使用(WordprocessingDocument fDocx=WordprocessingDocument.Open(sDocxFile,true))
{
字符串sChunkID=“myhtmlID”;
AlternativeFormatImportPart oChunk=fDocx.main DocumentPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.Html,sChunkID);
使用(FileStream fs=File.Open(sHtml、FileMode.OpenOrCreate))
{
oChunk.FeedData(fs);
}
AltChunk oAltChunk=新的AltChunk();
oAltChunk.Id=sChunkID;
//将html插入“H1”标记并删除H1。
Body Body=fDocx.main documentpart.Document.Body;
段落Paragraph=body.subjects()。其中(p=>p.InnerText==“H1”).FirstOrDefault();
插入到self后面(oAltChunk);
paragraph.Remove();
fDocx.MainDocumentPart.Document.Save();
}

仅提供信息是不够的。请与我们共享您的代码。拼写,格式化。请添加到目前为止您尝试的代码,并告诉我们您卡住的地方。现在添加代码代码代码正在运行但没有此代码的输出html标记部分未添加到我的现有文档FTANJS.docx文件中如何在word documnetso中添加html部分我在使用什么在现有docx文件中添加/附加html内容任何链接??@ASIFSHAIKH我没有任何具体的建议。你需要在谷歌上搜索并找到一个你喜欢的。你确实可以看到谢维克多的答案