问题
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