gue*_*rda 14 java docx apache-poi xwpf
我正在使用POI创建一个docx生成器,并希望使用预定义的格式.
Word包括多种格式,如标题,标题1..10等.这些格式是在您使用Word创建的每个DOCX中预定义的.
我想在我的docx生成器中使用它们.我尝试了以下但是格式没有应用:
paragraph = document.createParagraph();
lastParagraph.setStyle("Heading1");
Run Code Online (Sandbox Code Playgroud)
我还尝试了"标题1","heading1"和"Heading1"作为样式,但它们都没有奏效.
该API文档不显示任何信息.
我分析了用Word 2007创建的docx文件,发现"Heading1"是正确的.不幸的是,样式没有在docx中定义.我是否必须手动创建此样式?
谁能指出我正确的解决方案?
gue*_*rda 19
这很简单:使用"模板"docx文件.
XWPFDocument这是代码:
XWPFDocument document = new XWPFDocument(new FileInputStream("template.docx");
paragraph = document.createParagraph();
paragraph.setStyle("Heading1");
Run Code Online (Sandbox Code Playgroud)
该模板包含所有样式,因此可以通过引用setStyle("Heading1");.
您可以创建一个单词模板(只需使用Word中的另存为...功能).
该模板现在在\ word文件夹中包含许多其他XML文件: - styles.xml - stylesWithEffects.xml - webSettings.xml - fontTable.xml和 - \theme文件夹
如果将这些文件复制到原始POI生成的文件中,则可以引用styles.xml文件中给出的样式.
您可以像ZIP文件一样操作原始文件,这不应该花费太多精力.
将代码中的样式从模板复制到文档中:
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;
Run Code Online (Sandbox Code Playgroud)
从好的方面来说,您可以使用Word直接单独操作样式并将其保存回模板文件.
如果您通常对创建被识别为1级标题的样式感兴趣(例如,用于MS Word生成的TOC)并且可以在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);
}
Run Code Online (Sandbox Code Playgroud)
是的,此样式将显示在styles.xml中.
(我知道:这不是你问题的直接答案,但由于我没有在互联网上以可用的形式找到这些信息,我会在这里发布)
| 归档时间: |
|
| 查看次数: |
17612 次 |
| 最近记录: |