当使用OpenXML heading1样式正确运行时,我遇到了问题,但heading2样式不是。我之所以能够访问这些标题,是因为我正在复制一个模板word文档,该文档已经预装了样式。
用于创建标题的函数
public static Paragraph CreateHeading(string text, Body body, string type)
{
Paragraph para = body.AppendChild(new Paragraph());
Run run = para.AppendChild(new Run());
run.AppendChild(new Text(text));
para.ParagraphProperties = new ParagraphProperties(new ParagraphStyleId() { Val = type });
return para;
}
public CustomizationInformation GenerateDocumentation(WordprocessingDocument document, EntityMetadata metadata, CustomizationInformation customizations)
{
_metadata = metadata;
_customizations = customizations;
// Create our table
_table = TableFactory.CreateTable();
//ParagraphFactory.CreateHeading("Attributes", document.MainDocumentPart.Document.Body, "Heading1"); // **
ParagraphFactory.CreateHeading("Attributes", document.MainDocumentPart.Document.Body, "Heading2"); // **
document.MainDocumentPart.Document.Body.Append(ParagraphFactory.Create("The following attributes are exposed on this entity."));
// Initialize table
initializeTable();
addAttributes();
// Add our table to the document
document.MainDocumentPart.Document.Body.Append(_table);
return _customizations;
}*位于上述代码中的位置。Heading1函数工作正常,但是Heading2没有出现。
谢谢你的帮助我很感激。
发布于 2014-07-15 18:39:48
我做了一个测试,似乎在Word文档中使用样式时必须考虑几点:
Val属性ParagraphStyleId中指定的值不应被视为常量,即使对于基本样式(如标题),因为在MS Word的其他语言版本中,这些样式可以被不同的命名。您应该能够在StyleDefinitionsPart部件的文档中找到样式定义。您可以使用以下代码列出文档中定义的样式(只用于测试,我希望保持简单,但如果您想在应用程序中使用它,应该添加检查null值和处理部件集合中的多个元素):
var sDParts = document.MainDocumentPart.GetPartsOfType<StyleDefinitionsPart>();
foreach (var style in sDParts.First().Styles.ChildElements.OfType<Style>())
{
Console.WriteLine("Style id: {0}, style name: {1}",
style.StyleId,
style.StyleName.Val);
}我认为style.StyleId中的值设置应该用于PagraphStyleId元素中的Val属性。
https://stackoverflow.com/questions/24763834
复制相似问题