开发者

Inserting HTML into Word Using OpenXML

I have some HTML stored in a database that I want to insert into a Word document using D开发者_Python百科ocumentFormat.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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜