Inserting HTML into Word Using OpenXML

偶尔善良 提交于 2019-12-25 01:08:08

问题


I have some HTML stored in a database that I want to insert into a Word document using DocumentFormat.OpenXml.

Inspired by the article here, I tried the following code.

mainPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.Xhtml,
    String.Format("<html><body>{0}</body></html>", html));

But this gives me the following error.

'(My HTML Here)' ID is not a valid XSD ID

I really don't understand this error. Does anyone know what I'm doing wrong?

Also, my biggest concern about this approach is that the HTML may not be perfectly formed and I suspect this code is not as forgiving as browsers are. Any recommendations for other possible approaches? I'm considering parsing the HTML and rendering it myself, but that will be a lot of work.


回答1:


The second parameter is a part ID, not the part contents.

To set the part contents, you need to put well-formed XHTML into the RootElement property of the returned AlternativeFormatImportPart



来源:https://stackoverflow.com/questions/7015505/inserting-html-into-word-using-openxml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!