java 如何将 DOCX 中的预定义格式与 POI 一起使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2643822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I use predefined formats in DOCX with POI?
提问by guerda
I'm creating a docx generator with POI and would like to use predefined formats.
我正在创建一个带有 POI 的 docx 生成器,并希望使用预定义的格式。
Word includes several formats like Title, Heading 1..10 etc. These formats are predefined in every DOCX you create with Word.
Word 包括多种格式,如标题、标题 1..10 等。这些格式在您使用 Word 创建的每个 DOCX 中都是预定义的。
I would like to use them in my docx generator. I tried the following but the format was not applied:
我想在我的 docx 生成器中使用它们。我尝试了以下但未应用格式:
paragraph = document.createParagraph();
lastParagraph.setStyle("Heading1");
I also tried "heading 1", "heading1" and "Heading1" as style, but none of them worked.
The API documentationdoesn't show any details.
我还尝试将“标题 1”、“标题 1”和“标题 1”作为样式,但它们都不起作用。
该API文档不显示任何信息。
I analysed a docx file created with Word 2007 and found out "Heading1" would be correct. Unfortunately, the style is not defined in the docx. Do I have to create this style manually?
我分析了一个用 Word 2007 创建的 docx 文件,发现“Heading1”是正确的。不幸的是,该样式未在 docx 中定义。我必须手动创建这种样式吗?
Can anyone point me to the correct solution?
谁能指出我正确的解决方案?
回答by guerda
It's very simple: Use a "template" docx file.
很简单:使用“模板”docx 文件。
- Create an empty docx file with Word 2007.
- Use this file as a template for your
XWPFDocument - Add your paragraphs with the styles.
- 使用 Word 2007 创建一个空的 docx 文件。
- 使用此文件作为您的模板
XWPFDocument - 添加带有样式的段落。
Here's the code:
这是代码:
XWPFDocument document = new XWPFDocument(new FileInputStream("template.docx");
paragraph = document.createParagraph();
paragraph.setStyle("Heading1");
The template contains all styles and therefore they can referenced via setStyle("Heading1");.
该模板包含所有样式,因此可以通过setStyle("Heading1");.
回答by Drejc
You can create a word template (just use the Save as... feature in Word).
您可以创建一个单词模板(只需使用 Word 中的另存为...功能)。
first option
第一个选项
The template now contains a number of additional XML files in \wordfolder: - styles.xml - stylesWithEffects.xml - webSettings.xml - fontTable.xml and a - \theme folder
该模板现在在\word文件夹中包含许多额外的 XML 文件:-styles.xml-stylesWithEffects.xml-webSettings.xml-fontTable.xml 和一个-\theme 文件夹
If you copy those files into your original POI generated file then you can reference styles given in the styles.xmlfile.
如果您将这些文件复制到原始 POI 生成的文件中,那么您可以参考style.xml文件中给出的样式。
You can manipulate your original file like a ZIP file, this shouldn't be to much effort.
您可以像处理 ZIP 文件一样操作原始文件,这应该不会太费力。
second option
第二种选择
Copy styles in code from template to your document:
将代码中的样式从模板复制到您的文档:
XWPFDocument template = new XWPFDocument(new FileInputStream(new File("Template.dotx")));
XWPFDocument doc = new XWPFDocument();
// let's copy styles from template to new doc
XWPFStyles newStyles = doc.createStyles();
newStyles.setStyles(template.getStyle());
XWPFParagraph para = doc.createParagraph();
para.setStyle("Heading1");
XWPFRun run = para.createRun();
run.setText("Heading 1");
return doc;
On the plus side you can manipulate your styles separately directly using Word and saving them back to the template file.
从好的方面来说,您可以直接使用 Word 单独操作样式并将它们保存回模板文件。
回答by RobertG
If you are generally interested in creating a style that is recognized as a level 1 heading (e.g., for use in an MS Word-generated TOC) and can be accessed in the Word formats bar, it can be done like this:
如果您通常对创建被识别为 1 级标题(例如,用于 MS Word 生成的目录)并且可以在 Word 格式栏中访问的样式感兴趣,可以这样做:
private static File writeSimpleDocxFile(String content) throws IOException {
XWPFDocument docxDocument = new XWPFDocument();
String strStyleId = "ownstyle1";
addCustomHeadingStyle(docxDocument, strStyleId, 1);
XWPFParagraph paragraph = docxDocument.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText(content);
paragraph.setStyle(strStyleId);
}
private static void addCustomHeadingStyle(XWPFDocument docxDocument, String strStyleId, int headingLevel) {
CTStyle ctStyle = CTStyle.Factory.newInstance();
ctStyle.setStyleId(strStyleId);
CTString styleName = CTString.Factory.newInstance();
styleName.setVal(strStyleId);
ctStyle.setName(styleName);
CTDecimalNumber indentNumber = CTDecimalNumber.Factory.newInstance();
indentNumber.setVal(BigInteger.valueOf(headingLevel));
// lower number > style is more prominent in the formats bar
ctStyle.setUiPriority(indentNumber);
CTOnOff onoffnull = CTOnOff.Factory.newInstance();
ctStyle.setUnhideWhenUsed(onoffnull);
// style shows up in the formats bar
ctStyle.setQFormat(onoffnull);
// style defines a heading of the given level
CTPPr ppr = CTPPr.Factory.newInstance();
ppr.setOutlineLvl(indentNumber);
ctStyle.setPPr(ppr);
XWPFStyle style = new XWPFStyle(ctStyle);
// is a null op if already defined
XWPFStyles styles = docxDocument.createStyles();
style.setType(STStyleType.PARAGRAPH);
styles.addStyle(style);
}
Yes, this style will show up in the styles.xml.
是的,此样式将显示在styles.xml 中。
(I know: This is not a direct answer to your question, but as I did not find this info on the internet in a usable form, I'll post it here)
(我知道:这不是对您问题的直接回答,但由于我没有在互联网上以可用的形式找到此信息,我将其发布在这里)
回答by Roman
Yes, you should do it manually. Docx spec says that styles.xml which contains info about styles is optional. So, I almost sure that POI doesn't create it at all if you do not do it explicitely. You can check it: just unzip docx file and look whether this file is there or not (yourfile.docx/word/styles.xml).
是的,您应该手动执行此操作。Docx 规范说,包含样式信息的styles.xml 是可选的。因此,我几乎可以肯定,如果您不明确地这样做,POI 根本不会创建它。您可以检查它:只需解压缩 docx 文件并查看该文件是否存在(yourfile.docx/word/styles.xml)。
So, what you should do (in docx terms, I don't know how that's implemented in POI):
所以,你应该做什么(在 docx 术语中,我不知道这是如何在 POI 中实现的):
1) create styles.xml and add necessary styles there
1)创建styles.xml并在那里添加必要的样式
2) create relationship which connects document.xml and styles.xml (I think POI should do it automatically)
2)创建连接document.xml和styles.xml的关系(我认为POI应该自动完成)
3) use styles ids inside document.xml to connect concrete text part (Run in docx terms) with concrete style.
3) 使用 document.xml 中的样式 ID 将具体文本部分(以 docx 术语运行)与具体样式连接起来。

