Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将HTML转换为Word Docx且样式保持不变_C#_Openxml - Fatal编程技术网

C# 将HTML转换为Word Docx且样式保持不变

C# 将HTML转换为Word Docx且样式保持不变,c#,openxml,C#,Openxml,我知道已经有类似的问题了,建议使用开放式XML等等 我使用的是开放式XMl,但它只适用于内联样式 有没有解决这个问题的方法,或者有没有其他更好的方法将html转换为docx,而不是OpenXML 谢谢 您可以使用上述工具内联CSS文件 然后,要执行转换(改编自): 使用(wordprocessingdocumentmydoc= WordprocessingDocument.Open(“ConvertedDocument.docx”,true)) { 字符串altChunkId=“AltChunk

我知道已经有类似的问题了,建议使用开放式XML等等

我使用的是开放式XMl,但它只适用于内联样式

有没有解决这个问题的方法,或者有没有其他更好的方法将html转换为docx,而不是OpenXML


谢谢

您可以使用上述工具内联CSS文件

然后,要执行转换(改编自):

使用(wordprocessingdocumentmydoc=
WordprocessingDocument.Open(“ConvertedDocument.docx”,true))
{
字符串altChunkId=“AltChunkId1”;
MainDocumentPart mainPart=myDoc.MainDocumentPart;
var chunk=mainPart.AddAlternativeFormatImportPart(
AlternativeFormatImportPartType.Html,altChunkId);
使用(FileStream FileStream=File.Open(“YourHtmlDocument.html”,FileMode.Open))
{
FeedData(fileStream);
}
AltChunk AltChunk=new AltChunk(){Id=AltChunk Id};
mainPart.Document.Body.InsertAfter(
altChunk,mainPart.Document.Body.Elements().Last());
mainPart.Document.Save();
}
这并不是将HTML转换为DOCX。它将
YourHtmlDocument.html
附加到
ConvertedDocument.docx
。如果
ConvertedDocument.docx
最初为空,则此方法实际上是一种转换


无论何时使用
AltChunk
构建文档,您的HTML都将嵌入文档中,直到下一次在Word中打开文档。此时,HTML将转换为
WordProcessingML
标记。这实际上只是一个问题,如果文档不能在MS Word中打开。如果你上传到谷歌文档,在OpenOffice中打开,或者使用COM转换成PDF,OpenXML是不够的。在这种情况下,您可能需要求助于付费工具,如。

这会在
元素().Last()
上崩溃,如果前面没有插入段落,则无法找到
Last()
。使用
mainPart.Document.Body.InsertAfterSelf(altChunk)代替如何在word文档的页眉和页脚中插入altchunk?
using (WordprocessingDocument myDoc =
    WordprocessingDocument.Open("ConvertedDocument.docx", true))
{
    string altChunkId = "AltChunkId1";
    MainDocumentPart mainPart = myDoc.MainDocumentPart;
    var chunk = mainPart.AddAlternativeFormatImportPart(
        AlternativeFormatImportPartType.Html, altChunkId);

    using (FileStream fileStream = File.Open("YourHtmlDocument.html", FileMode.Open))
    {
        chunk.FeedData(fileStream);
    }
    AltChunk altChunk = new AltChunk() {Id = altChunkId};

    mainPart.Document.Body.InsertAfter(
               altChunk, mainPart.Document.Body.Elements<Paragraph>().Last());
    mainPart.Document.Save();
}