【发布时间】:2019-10-09 15:10:32
【问题描述】:
我正在从一个 html 字符串生成一个 word docx 文件,并使用我发现的各种文章来向该文档添加简单的页眉和页脚。 我现在需要做的是也从 html 字符串生成标题,但我正在努力寻找如何完成此操作的示例。 这个问题的作者: Export docx/doc First Header and Footer as docx File Using openXML 说他已经做到了,但不幸的是他没有发布示例。
我下面的代码成功地添加了一个标题,但是从代码中可以看出,它只会将 html 作为文本添加到标题中,而我想传入一个 html 字符串,并且该字符串以格式化的 html 出现在标题中。
有人做过吗?
static void AddHeaderPart(MainDocumentPart mainPart, string headerHtml, Encoding encoding)
{
if (mainPart == null || string.IsNullOrEmpty(headerHtml))
{return;}
// Create a new header part.
HeaderPart headerPart = mainPart.AddNewPart<HeaderPart>();
// Get Id of the headerPart.
string rId = mainPart.GetIdOfPart(headerPart);
// Call GenerateHeaderPartContent
GenerateHeaderPartContent(mainPart, headerPart, headerHtml, encoding);
// Get SectionProperties and Replace HeaderReference with new Id.
IEnumerable<SectionProperties> sectPrs = mainPart.Document.Body.Elements<SectionProperties>();
foreach (var sectPr in sectPrs)
{
// Delete existing references to headers.
sectPr.RemoveAllChildren<HeaderReference>();
// Create the new header reference node.
sectPr.PrependChild<HeaderReference>(new HeaderReference() { Id = rId, Type = HeaderFooterValues.Default });
}
}
static void GenerateHeaderPartContent(MainDocumentPart mainPart, HeaderPart headerPart, string headerHtml, Encoding encoding)
{
Header header1 = new Header() { MCAttributes = new MarkupCompatibilityAttributes() { Ignorable = "w14 wp14" } };
header1.AddNamespaceDeclaration("wpc", "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas");
header1.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
header1.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office");
header1.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
header1.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math");
header1.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml");
header1.AddNamespaceDeclaration("wp14", "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing");
header1.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
header1.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word");
header1.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
header1.AddNamespaceDeclaration("w14", "http://schemas.microsoft.com/office/word/2010/wordml");
header1.AddNamespaceDeclaration("wpg", "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup");
header1.AddNamespaceDeclaration("wpi", "http://schemas.microsoft.com/office/word/2010/wordprocessingInk");
header1.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml");
header1.AddNamespaceDeclaration("wps", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape");
Paragraph paragraph1 = new Paragraph();
ParagraphProperties paragraphProperties1 = new ParagraphProperties();
ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId() { Val = "Header" };
paragraphProperties1.Append(paragraphStyleId1);
//This adds the headerHtml as text - how to add it as html?
Run run1 = new Run();
Text text1 = new Text();
text1.Text = headerHtml;
run1.Append(text1);
paragraph1.Append(paragraphProperties1);
paragraph1.Append(run1);
header1.Append(paragraph1);
headerPart.Header = header1;
}
【问题讨论】:
-
您必须将 html 转换为格式正确的 word 格式。 ooxml sdk 不会为你做这些
-
链接的问题与你的问题无关。那个人询问如何从 Word 文档生成 HTML。您要求的是相反的事情 - 如何解析 HTML 并在 Word 中生成等效样式。 This is a better question。这两个答案都显示了如何使用
altChunk将 HTML 块导入文档。但不能保证您会得到相同的外观 - HTML 样式不是 Word 样式 -
谢谢,但这是 P Kanavos 链接的同一篇文章,我曾经在上面找到它 - 我的答案发布在下面
标签: c# html ms-word openxml-sdk