XWPFDocument 、XWPFParagraph设置段落标题
在开发中遇到使用XWPFDocument 、XWPFParagraph设置标题不起作用的问题,参考了网上的一下资料归纳了以下步骤
第一步:新建一个word文档作为模板,新建一行随便输入内容,设置为一级标题,有需要的话可以换行输入内容设置二级标记等等。
效果如下
第二步编码:
代码如下:
public class WordUtil {
private CTStyles styles = null;
public void setWord(){
//模板地址
XWPFDocument model = new XWPFDocument(new FileInputStream("D:\\xxx.docx"));
styles = model.getStyle();
// 新建的word文档对象
XWPFDocument doc = new XWPFDocument();
// 获取新建文档样式
XWPFStyles docStyle = doc.createStyles();
// 设置新建文档样式为模板中读取到的样式
docStyle.setStyles(styles);
XWPFParagraph para = doc.createParagraph();
//设置一级标题(注意这里有问题,请往下看)
para.setStyle("2");
XWPFRun run = para.createRun();
// 标题内容
run.setText("一级标题");
OutputStream os = new FileOutputStream("D:\\simpleWrite.docx");
//把doc输出到输出流
doc.write(os);
os.close();
}
}
有一些人就开始疑惑为什么这里 para.setStyle(“2”)中的参数是2,而且有部分人发现 para.setStyle(“2”)中设置2并不起作用。至于在para.setStyle(“2”)中为什么设置的参数是2,在这里解释一下。
因为不同的word模板读取出来的style是不同的,输出读取到的模板样式styles,查看一下内容
<xml-fragment mc:Ignorable="w14" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:sl="http://schemas.openxmlformats.org/schemaLibrary/2006/main" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wpsCustomData="http://www.wps.cn/officeDocument/2013/wpsCustomData">
<w:docDefaults>
<w:rPrDefault>
<w:rPr>
<w:rFonts w:asciiTheme="minorHAnsi" w:cstheme="minorBidi" w:eastAsiaTheme="minorEastAsia" w:hAnsiTheme="minorHAnsi"/>
</w:rPr>
</w:rPrDefault>
<w:pPrDefault/>
</w:docDefaults>
......(省略内容)
<w:style w:default="1" w:styleId="1" w:type="paragraph">
<w:name w:val="Normal"/>
<w:qFormat/>
<w:uiPriority w:val="0"/>
<w:pPr>
<w:widowControl w:val="0"/>
<w:jc w:val="both"/>
</w:pPr>
<w:rPr>
<w:rFonts w:asciiTheme="minorHAnsi" w:cstheme="minorBidi" w:eastAsiaTheme="minorEastAsia" w:hAnsiTheme="minorHAnsi"/>
<w:kern w:val="2"/>
<w:sz w:val="21"/>
<w:szCs w:val="24"/>
<w:lang w:bidi="ar-SA" w:eastAsia="zh-CN" w:val="en-US"/>
</w:rPr>
</w:style>
<w:style w:styleId="2" w:type="paragraph">
<w:name w:val="heading 1"/>
<w:basedOn w:val="1"/>
<w:next w:val="1"/>
<w:qFormat/>
<w:uiPriority w:val="0"/>
<w:pPr>
<w:keepNext/>
<w:keepLines/>
<w:spacing w:after="330" w:afterAutospacing="0" w:afterLines="0" w:before="340" w:beforeAutospacing="0" w:beforeLines="0" w:line="576" w:lineRule="auto"/>
<w:outlineLvl w:val="0"/>
</w:pPr>
<w:rPr>
<w:b/>
<w:kern w:val="44"/>
<w:sz w:val="44"/>
</w:rPr>
</w:style>
<w:style w:styleId="3" w:type="paragraph">
<w:name w:val="heading 2"/>
<w:basedOn w:val="1"/>
<w:next w:val="1"/>
<w:unhideWhenUsed/>
<w:qFormat/>
<w:uiPriority w:val="0"/>
<w:pPr>
<w:keepNext/>
<w:keepLines/>
<w:spacing w:after="260" w:afterAutospacing="0" w:afterLines="0" w:before="260" w:beforeAutospacing="0" w:beforeLines="0" w:line="413" w:lineRule="auto"/>
<w:outlineLvl w:val="1"/>
</w:pPr>
<w:rPr>
<w:rFonts w:ascii="Arial" w:eastAsia="黑体" w:hAnsi="Arial"/>
<w:b/>
<w:sz w:val="32"/>
</w:rPr>
</w:style>
<w:style w:styleId="4" w:type="paragraph">
<w:name w:val="heading 3"/>
<w:basedOn w:val="1"/>
<w:next w:val="1"/>
<w:unhideWhenUsed/>
<w:qFormat/>
<w:uiPriority w:val="0"/>
<w:pPr>
<w:keepNext/>
<w:keepLines/>
<w:spacing w:after="260" w:afterAutospacing="0" w:afterLines="0" w:before="260" w:beforeAutospacing="0" w:beforeLines="0" w:line="413" w:lineRule="auto"/>
<w:outlineLvl w:val="2"/>
</w:pPr>
<w:rPr>
<w:b/>
<w:sz w:val="32"/>
</w:rPr>
</w:style>
<w:style w:styleId="5" w:type="paragraph">
<w:name w:val="heading 4"/>
<w:basedOn w:val="1"/>
<w:next w:val="1"/>
<w:unhideWhenUsed/>
<w:qFormat/>
<w:uiPriority w:val="0"/>
<w:pPr>
<w:keepNext/>
<w:keepLines/>
<w:spacing w:after="290" w:afterAutospacing="0" w:afterLines="0" w:before="280" w:beforeAutospacing="0" w:beforeLines="0" w:line="372" w:lineRule="auto"/>
<w:outlineLvl w:val="3"/>
</w:pPr>
<w:rPr>
<w:rFonts w:ascii="Arial" w:eastAsia="黑体" w:hAnsi="Arial"/>
<w:b/>
<w:sz w:val="28"/>
</w:rPr>
</w:style>
<w:style w:default="1" w:styleId="7" w:type="character">
<w:name w:val="Default Paragraph Font"/>
<w:semiHidden/>
<w:uiPriority w:val="0"/>
</w:style>
<w:style w:default="1" w:styleId="6" w:type="table">
<w:name w:val="Normal Table"/>
<w:semiHidden/>
<w:qFormat/>
<w:uiPriority w:val="0"/>
<w:tblPr>
<w:tblCellMar>
<w:top w:type="dxa" w:w="0"/>
<w:left w:type="dxa" w:w="108"/>
<w:bottom w:type="dxa" w:w="0"/>
<w:right w:type="dxa" w:w="108"/>
</w:tblCellMar>
</w:tblPr>
</w:style>
</xml-fragment>
找到以下内容
<w:style w:styleId="2" w:type="paragraph">
<w:name w:val="heading 1"/>
<w:basedOn w:val="1"/>
<w:next w:val="1"/>
<w:qFormat/>
<w:uiPriority w:val="0"/>
<w:pPr>
<w:keepNext/>
<w:keepLines/>
<w:spacing w:after="330" w:afterAutospacing="0" w:afterLines="0" w:before="340" w:beforeAutospacing="0" w:beforeLines="0" w:line="576" w:lineRule="auto"/>
<w:outlineLvl w:val="0"/>
</w:pPr>
<w:rPr>
<w:b/>
<w:kern w:val="44"/>
<w:sz w:val="44"/>
</w:rPr>
</w:style>
其中第二行中 <w:name w:val=“heading 1”/>的heading 1表示的就是一级标题,对应的styleId就是2
所以设置一级标题时在代码中这样写:para.setStyle(“2”)
当然设置标题的方式并非只有这种,也可以自定义标题样式,,在这里就不细说了。