无法在Open Xml中使用现有段落样式
我正在将HTML文件导出到Open XML wordfile。 如果在HTML
中使用,我想在该部分添加Heading1样式。 但不知何故,当我在Microsoft Word 2010中打开文档时,未应用样式。
如果我在Libre Office中打开创建的文档,则会应用一些样式。
我自己也定义了一些样式,如果我使用其中一种样式,Word和Libre Office中的一切都很顺利。
我打开了适用于Microsoft Office的Open XML SDK 2.5生产力工具 ,当我查看它提供的示例代码时,它建议:
ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId(){ Val = "Kop1" };
Kop1(而不是Heading1)是因为我的Word是荷兰语所以我的模板是荷兰语,但这并没有解决问题。
在此代码示例中,我创建了段落并向其添加样式和文本:
using (wordDocument = WordprocessingDocument.Open(documentStream, true)) { MainDocumentPart mainPart = wordDocument.MainDocumentPart; WP.Body body = wordDocument.MainDocumentPart.Document.Body; WP.Paragraph para = body.AppendChild(new WP.Paragraph()); StyleDefinitionsPart part = wordDocument.MainDocumentPart.StyleDefinitionsPart; if (part != null) { WP.ParagraphProperties pPr = new WP.ParagraphProperties(); WP.ParagraphStyleId paragraphStyleId1 = new WP.ParagraphStyleId() { Val = "Heading1" }; pPr.Append(paragraphStyleId1); para.Append(pPr); } WP.Run run = para.AppendChild(new WP.Run()); run.AppendChild(new WP.Text(value)); }
它定义了样式,因为我使用模板,所以代码将进入if语句。
我也尝试了它的风格标题 (荷兰语标题 ),试过两个但都没有工作…
我真的不明白什么是错的,为什么我可以使用我自己创建的样式但不能使用Word中的一种预定义样式,我没有在模板中删除。
它以某种方式无法识别预定义的样式?
编辑/添加
我得到它的工作,但不是我想要的方式,我必须单击模板文档中的预定义样式,然后保存文档。 不知怎的,现在点击它们并保存这些样式后会添加到文档中,因为如果我现在使用模板生成我的最终文档,我可以使用这些样式。 但是没有先点击文档中的样式,我不能使用它们,因为它们没有保存?
不幸的是,这是因为默认情况下Style没有写入文档(即使它是“内置”到Word),因此不应用样式。
如果您通过代码创建文档,然后通过Word应用Heading1样式创建文档,那么比较\ word \ styles.xml中的XML,您将看到Word生成的XML中还有一个附加部分:
您可以使用以下代码生成自己的样式信息。 我不知道从硬编码以外的地方获取这些样式的方法; 也许他们可以在某个地方找到一个dotx文件? 有关详细信息,请参阅下面的编辑
using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(documentStream, true)) { MainDocumentPart mainPart = wordDocument.MainDocumentPart; Body body = wordDocument.MainDocumentPart.Document.Body; Paragraph para = body.AppendChild(new Paragraph()); StyleDefinitionsPart part = wordDocument.MainDocumentPart.StyleDefinitionsPart; if (part != null) { Style style = new Style() { Type = StyleValues.Paragraph, StyleId = "Heading1", BasedOn = new BasedOn() { Val = "Normal" }, NextParagraphStyle = new NextParagraphStyle() { Val = "Normal" } }; StyleName styleName1 = new StyleName() { Val = "heading 1" }; style.Append(styleName1); style.Append(new PrimaryStyle()); StyleRunProperties styleRunProperties1 = new StyleRunProperties(); styleRunProperties1.Append(new Bold()); styleRunProperties1.Append(new RunFonts() { ComplexScriptTheme=ThemeFontValues.MajorBidi, HighAnsiTheme=ThemeFontValues.MajorHighAnsi, EastAsiaTheme=ThemeFontValues.MajorEastAsia, AsciiTheme=ThemeFontValues.MajorAscii }); styleRunProperties1.Append(new FontSize() { Val = "28" }); styleRunProperties1.Color = new Color() { Val = "365F91", ThemeShade = "BF", ThemeColor = ThemeColorValues.Accent1 }; style.Append(styleRunProperties1); part.Styles.Append(style); ParagraphProperties pPr = new ParagraphProperties(); ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId() { Val = "Heading1" }; pPr.Append(paragraphStyleId1); para.Append(pPr); } Run run = para.AppendChild(new Run()); run.AppendChild(new Text("This is a heading")); }
编辑
我看了一下,发现Word默认样式确实存储在.dotx文件中,如上所述。 这些文件也可以通过OpenXML API读取,因此可以将样式从那里复制到文档中。 这个代码非常类似于我们已经拥有的代码,添加了读取Default.dotx文件并从那里获取样式。
在我的系统上,dotx文件位于C:\ Program Files \ Microsoft Office \ Office14 \ 1033 \ QuickStyles中,我在下面对其进行了硬编码。 我不知道是否有某个地方可以动态拾取(也许是注册表),但如果不是,我猜配置文件就足够了。
using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(documentStream, true)) { MainDocumentPart mainPart = wordDocument.MainDocumentPart; Body body = wordDocument.MainDocumentPart.Document.Body; Paragraph para = body.AppendChild(new Paragraph()); Paragraph para2 = body.AppendChild(new Paragraph()); Paragraph para3 = body.AppendChild(new Paragraph()); StyleDefinitionsPart part = wordDocument.MainDocumentPart.StyleDefinitionsPart; if (part != null) { //I'm looping the styles and adding them here //you could clone the whole StyleDefinitionsPart //but then you'd lose custom styles in your source doc using (WordprocessingDocument wordTemplate = WordprocessingDocument.Open(@"C:\Program Files\Microsoft Office\Office14\1033\QuickStyles\default.dotx", false)) { foreach (var templateStyle in wordTemplate.MainDocumentPart.StyleDefinitionsPart.Styles) { part.Styles.Append(templateStyle.CloneNode(true)); } } //I can now use any built in style //I'm using Heading1, Title and IntenseQuote as examples //You may need to do a language conversion here as //you mentione your docx is in Dutch ParagraphProperties pPr = new ParagraphProperties(); ParagraphStyleId paragraphStyleId1 = new ParagraphStyleId() { Val = "Heading1" }; pPr.Append(paragraphStyleId1); para.Append(pPr); Run run = para.AppendChild(new Run()); run.AppendChild(new Text("This is a heading")); ParagraphProperties pPr2 = new ParagraphProperties(); ParagraphStyleId paragraphStyleId2 = new ParagraphStyleId() { Val = "Title" }; pPr2.Append(paragraphStyleId2); para2.Append(pPr2); Run run2 = para2.AppendChild(new Run()); run2.AppendChild(new Text("This is a title")); ParagraphProperties pPr3 = new ParagraphProperties(); ParagraphStyleId paragraphStyleId3 = new ParagraphStyleId() { Val = "IntenseQuote" }; pPr3.Append(paragraphStyleId3); para3.Append(pPr3); Run run3 = para3.AppendChild(new Run()); run3.AppendChild(new Text("This is an intense quote")); } }
这个产生的文件看起来像这样:
- 如何以编程方式设置或清除32BIT标志?
- 访问HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Installer \ UserData
- 如何在VS2010中允许打破’System.NullReferenceException’?
- 希望Autofac不注册任何具有多个实现的接口
- 使用.NET / WMI获取PC的监视器信息
- 根据属性将元素反序列化为属性
- WPF应用程序的基于角色的访问控制 – 最佳实践
- 升级到ASP.NET Core 2.0后,无法从singleton IActiveUsersService使用作用域服务IMongoDbContext
- C#App.config与设置文件