XWPFDocument对象
XWPFDocument:代表一个docx文档
XWPFParagraph:代表文档、表格、标题等种的段落,由多个XWPFRun组成
XWPFRun:代表具有同样风格的一段文本
XWPFTable:代表一个表格
XWPFTableRow:代表表格的一行
XWPFTableCell:代表表格的一个单元格
XWPFChar:表示.docx文件中的图表
XWPFHyperlink:表示超链接
XWPFPicture:代表图片
XWPFComment :代表批注
XWPFFooter:代表页脚
XWPFHeader:代表页眉
XWPFParagraph段落属性
xwpfparagraph.setAlignment(ParagraphAlignment.RIGHT);//设置对齐方式
xwpfparagraph.setFontAlignment(2);//段落对齐方式
xwpfparagraph.setVerticalAlignment(TextAlignment.BOTTOM);//文本垂直对齐方式(作用于表格)
xwpfparagraph.setWordWrapped(true);//设置文字环绕(当文字或者英文单词在一行显示不全时,是否移动到下一行)
xwpfparagraph.setNumID(BigInteger.valueOf(1));//设置段落编号(暂不清楚效果)
xwpfparagraph.setPageBreak(true);//设置分页(每一段内容都为新的一页的开始,表格新增数据时会新增一页)
xwpfparagraph.setBorderTop(Borders.DOT_DASH);//设置边框-上
xwpfparagraph.setBorderBottom(Borders.DOT_DASH);//设置边框-下
xwpfparagraph.setBorderLeft(Borders.DOT_DASH);//设置边框-左
xwpfparagraph.setBorderRight(Borders.DOT_DASH);//设置边框-右
xwpfparagraph.setBorderBetween(Borders.SINGLE);//设置段落之间边框(当两个段落之间分别设置上下边框时,即展示两个段落之间)
xwpfparagraph.setFirstLineIndent(400);//首行缩进(400-0.71cm)
xwpfparagraph.setIndentationFirstLine(400);//首行缩进(400-0.71cm)
xwpfparagraph.setIndentationHanging(400);//首行前进(400-0.71cm)
xwpfparagraph.setIndentationLeft(400);//整行左边缩进(400-0.71cm)
xwpfparagraph.setIndentationRight(400);//整行右边缩进(400-0.71cm)
xwpfparagraph.setIndentFromLeft(400);//整行左边缩进(400-0.71cm)
xwpfparagraph.setIndentFromRight(400);//整行右边缩进(400-0.71cm)
xwpfparagraph.setSpacingAfter(200);//设置段后间距(200-10磅)单位磅
xwpfparagraph.setSpacingAfterLines(400);//设置段后间距(400-4行)单位行
xwpfparagraph.setSpacingBefore(200);//设置段前间距(200-10磅)单位磅
xwpfparagraph.setSpacingBeforeLines(200);//设置段前间距(200-2行)单位行
xwpfparagraph.setSpacingBetween(3.1,LineSpacingRule.AT_LEAST);//设置多倍行距(20-2倍)-多倍行距AUTO(3-3倍),固定值EXACT(10-120磅),最小值AT_LEAST(10-120磅)
xwpfparagraph.setSpacingLineRule(LineSpacingRule.AUTO);//设置间距规则(AUTO-多倍行距,EXACT-固定值,AT_LEAST-最小值)
XWPFRun属性
//导出默认格式为(黑色,宋体,五号,两端对齐,单倍行距)
xwpfrun.setText("测试文本");//设置文本内容
xwpfrun.setFontSize(30);//设置文本字号
xwpfrun.setFontFamily("仿宋", XWPFRun.FontCharRange.cs);//设置文本字体,FontCharRange字符字体系列(非必要)
xwpfrun.setBold(true);//设置文本加粗
//字符间距
xwpfrun.setTextScale(80);设置文本比例(80%)
xwpfrun.setCharacterSpacing(100);//设置文本字符间距(100~0.18cm)
xwpfrun.setTextPosition(200);//设置文本字符位置(200~3.53cm)
xwpfrun.setKerning(200);//设置字体字间距(200~100磅)
//字体效果
xwpfrun.setColor("FF0000");//设置文本颜色(十六进制)
xwpfrun.setItalic(true);//设置斜体
xwpfrun.setStrikeThrough(true);//设置文本删除线(效果:删除线)
xwpfrun.setDoubleStrikethrough(true);//设置文本双删除线(效果:双删除线)
xwpfrun.setSubscript(VerticalAlign.BASELINE);//设置下标(BASELINE-默认,SUPERSCRIPT-上标,SUBSCRIPT-下标);例如:上标-35°、下标-O₂
xwpfrun.setShadow(true);//设置阴影(效果:阴影)
xwpfrun.setEmbossed(true);//设置浮雕字体(效果:阳文)
xwpfrun.setImprinted(true);//设置印记(效果:阴文)
xwpfrun.setSmallCaps(true);//设置小型大写字母(效果:小型大写字母)
xwpfrun.setCapitalized(true);//设置全部大写字母(效果:全部大写字母a->A)
xwpfrun.setVanish(true);//设置文字隐藏(效果:隐藏文字)
xwpfrun.setVerticalAlignment("baseline");//设置文本对齐方式(baseline-默认,superscript-上标,subscript-下标);例如:上标-35°、下标-O₂
xwpfrun.setEmphasisMark("circle");//设置标记(none-默认,dot-小圆点,comma-逗号,circle-圆圈,underDot-下点)
xwpfrun.setUnderline(UnderlinePatterns.THICK);//设置下划线
xwpfrun.setUnderlineColor("FFFFFF");//设置下划线颜色
xwpfrun.setUnderlineThemeColor("FFFFFC");//设置下划线主题颜色
xwpfrun.setTextHighlightColor("FF0000");//设置文本突出显示颜色
//整体效果
xwpfrun.addBreak();//文本换行
xwpfrun.addCarriageReturn();//回车键类似于换行
xwpfrun.addTab();//TAB键
XWPFTable表格
xwpftable.addNewCol();//为该表中的每一行添加一个新列
xwpftable.addRow(XWPFTableRow row);//向表中添加新行
xwpftable.addRow(XWPFTableRow row, int pos);//在pos位置向表添加新行
xwpftable.setWidth("100.2%"); //设置宽度
xwpftable.setTableAlignment(TableRowAlign.CENTER); //对齐方式
xwpftable.getCTTbl().getTblPr(); //获取表格格式
xwpftable.createRow();//创建一个新的XWPFTableRow对象,该对象的单元格数与当时定义的列数相同
xwpftable.getBottomBorderColor();//获取底部边框颜色
xwpftable.getBottomBorderSize();//获取底部边框大小
xwpftable.getBottomBorderSpace();//获取底部边框间距
xwpftable.getBottomBorderType();//获取底部边框类型
xwpftable.getCellMarginBottom();
xwpftable.getCellMarginBottom();
xwpftable.getCellMarginLeft();
xwpftable.getCellMarginRight();
xwpftable.getCellMarginTop();
xwpftable.getColBandSize();
xwpftable.getElementType();//返回BodyElement表的类型
xwpftable.getInsideHBorderColor();//获取水平边框颜色
xwpftable.getInsideHBorderSize();//获取水平边框大小
xwpftable.getInsideHBorderSpace();//获取水平边框间距
xwpftable.getInsideHBorderType(); //获取水平边框类型
xwpftable.getInsideVBorderColor(); //获取垂直边框颜色
xwpftable.getInsideVBorderSize(); //获取垂直边框大小
xwpftable.getInsideVBorderSpace(); //获取垂直边框间距
xwpftable.getInsideVBorderType(); //获取垂直边框类型
xwpftable.getLeftBorderColor(); //获取左边框颜色
xwpftable.getRow(); //获取行
表格样式常用逻辑:
XWPFTable xwpftable= document.createTable(keyName.size(), 6); //代表一个表格
CTTblPr tblPr = xwpftable.getCTTbl().getTblPr();
CTTblLayoutType t = tblPr.isSetTblLayout() ? tblPr.getTblLayout() : tblPr.addNewTblLayout();
t.setType(STTblLayoutType.FIXED); //使表格宽度变得固定,不再因为内容而变宽