简介
Word编程最重要的类是org.apache.poi.xwpf.usermodel.XWPFDocument。涉及的东西十分复杂。而且Apache poi操作word的技术非常不成熟。代码中本身有很多bug。
Maven的依赖为
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.0.0</version>
</dependency>
以下代码创建一个空word文档。
public class EmptyWord {
public static void main(String[] args) throws IOException {
XWPFDocument document = new XWPFDocument();
File file = new File("test.word");
document.write(new FileOutputStream(file));
}
}
段落
首先看看段落与字体设置。
假设有需要生成一个一级标题,利用document创建段落。每个段落又有多个run组成。Run不能继续拆分,一个run拥有共同的字体。如以下代码创建一个段落:
final XWPFParagraph paragraph = document.createParagraph();
paragraph.setNumILvl(BigInteger.valueOf(1L));
final XWPFRun run = paragraph.createRun();
run.setText("老了");
run.setFontSize(10);
run.setColor("ffff00");
run.setFontFamily("宋书");
而段落的大纲级别的设置比较复杂,代码如下
CTPPr pPr = paragraph.getCTP().getPPr();
if (pPr == null) {
pPr = paragraph.getCTP().addNewPPr();
}
final CTDecimalNumber ctDecimalNumber = pPr.addNewOutlineLvl();
ctDecimalNumber.setVal(BigInteger.valueOf(1));
pPr.setOutlineLvl(ctDecimalNumber);
这里有一个难懂的概念,什么是CTP。
其效果如下:
页头与页脚
页头与页脚测试时发现生成的页头和页脚只能在WORD中看到,在WPS里看不到。这可能是POI的一个bug。生成页头和页脚都比较简单。
final XWPFDocument document = new XWPFDocument();
final XWPFHeader header = document.createHeader(HeaderFooterType.DEFAULT);
final XWPFParagraph paragraph = header.createParagraph();
final XWPFRun run = paragraph.createRun();
run.setText("我是页头");
run.setFontSize(12);
run.setColor("ff00ff");
System.out.println(header.getText());
页脚为:
// 页脚呢
final XWPFFooter footer = document.createFooter(HeaderFooterType.DEFAULT);
final XWPFParagraph footerParagraph = footer.createParagraph();
final XWPFRun footerParagraphRun = footerParagraph.createRun();
footerParagraphRun.setText("页脚");
footerParagraphRun.setFontSize(12);
完整效果如下:
页码
生成页码的方法比较复杂。但是值得挑战一下。
final XWPFFooter footer = document.createFooter(HeaderFooterType.DEFAULT);
XWPFParagraph paragraph = footer.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("第");
run = paragraph.createRun();
CTFldChar ctFldChar = run.getCTR().addNewFldChar();
ctFldChar.setFldCharType(STFldCharType.BEGIN);
// 又一段
run = paragraph.createRun();
CTText ctText = run.getCTR().addNewInstrText();
ctText.setStringValue("PAGE \\* MERGEFORMAT");
ctText.setSpace(SpaceAttribute.Space.Enum.forString("preserve"));
ctFldChar = run.getCTR().addNewFldChar();
ctFldChar.setFldCharType(STFldCharType.END);
run = paragraph.createRun();
run.setText("页 总共");
run = paragraph.createRun();
ctFldChar = run.getCTR().addNewFldChar();
ctFldChar.setFldCharType(STFldCharType.BEGIN);
run = paragraph.createRun();
ctText = run.getCTR().addNewInstrText();
ctText.setStringValue("NUMPAGES \\* MERGEFORMAT");
ctText.setSpace(SpaceAttribute.Space.Enum.forString("preserve"));
ctFldChar = run.getCTR().addNewFldChar();
ctFldChar.setFldCharType(STFldCharType.END);
run = paragraph.createRun();
run.setText("页");
同样,兼容word,不兼容WPS。
效果如下:
表格
Word里插入表格,是非常常见的功能。
final XWPFDocument document = new XWPFDocument();
final XWPFTable table = document.createTable(3, 3);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
table.getRow(i).getCell(j).setText(i+"-"+j);
}
}
表格有点丑,但是勉强可以用哈:
图片
插入图片也是必要的功能啊,代码示例如下:
String imagePath = "image.png"; // 图片路径
FileInputStream imageStream = new FileInputStream(imagePath);
// 设置图片尺寸(单位:EMU)
int width = Units.toEMU(300); // 宽度(约4厘米)
int height = Units.toEMU(200); // 高度
final XWPFParagraph paragraph = document.createParagraph();
final XWPFRun run = paragraph.createRun();
// 插入图片
run.addPicture(
imageStream,
XWPFDocument.PICTURE_TYPE_PNG, // 图片格式
"image.png", // 描述文本
width,
height
);
imageStream.close();
插入图片效果:
批注
Word编程加批注是十分困难的、十分复杂的。在poi里,有同名的包,不能导错,以下是正确的包:
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTComment;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTComments;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;
首先要写一个辅助类,辅助类倒是比较简单:
public class MyXWPFCommentsDocument extends POIXMLDocumentPart {
private CTComments ctComments;
private MyXWPFCommentsDocument(PackagePart part) {
super(part);
ctComments = CommentsDocument.Factory.newInstance().addNewComments();
}
public CTComments getCtComments() {
return ctComments;
}
@Override
protected void commit() throws IOException {
XmlOptions xmlOptions = new XmlOptions(POIXMLTypeLoader.DEFAULT_XML_OPTIONS);
xmlOptions.setSaveSyntheticDocumentElement(new QName(CTComments.type.getName().getNamespaceURI(), "comments"));
PackagePart part = getPackagePart();
OutputStream out = part.getOutputStream();
ctComments.save(out, xmlOptions);
out.close();
}
public static MyXWPFCommentsDocument createCommentsDocument(XWPFDocument document) throws Exception {
OPCPackage oPCPackage = document.getPackage();
PackagePartName partName = PackagingURIHelper.createPartName("/word/comments.xml");
PackagePart part = oPCPackage.createPart(partName, "application/vnd.openxmlformats-officedocument.wordprocessingml.comments+xml");
MyXWPFCommentsDocument myXWPFCommentsDocument = new MyXWPFCommentsDocument(part);
String rId = "rId" + (document.getRelationParts().size()+1);
document.addRelation(rId, XWPFRelation.COMMENT, myXWPFCommentsDocument);
return myXWPFCommentsDocument;
}
}
以下是加入批注的方法:
MyXWPFCommentsDocument myXWPFCommentsDocument = MyXWPFCommentsDocument.createCommentsDocument(document);
CTComments comments = myXWPFCommentsDocument.getCtComments();
CTComment ctComment;
XWPFParagraph paragraph;
//first comment
BigInteger cId = BigInteger.ZERO;
ctComment = comments.addNewComment();
CTText ctText = ctComment.addNewP().addNewR().addNewT();
ctText.setStringValue("The first comment.");
ctComment.setAuthor("Axel Ríchter");
ctComment.setInitials("AR");
ctComment.setId(cId);
paragraph = document.createParagraph();
paragraph.getCTP().addNewCommentRangeStart().setId(cId);
XWPFRun run;
run = paragraph.createRun();
run.setText("Paragraph with the first comment.");
paragraph.getCTP().addNewCommentRangeEnd().setId(cId);
paragraph.getCTP().addNewR().addNewCommentReference().setId(cId);
以下是批注的效果:
文本框
文本框的插入也是比较复杂,代码如下:
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run=paragraph.createRun();
run.setText("The Body text: ");
CTGroup ctGroup = CTGroup.Factory.newInstance();
CTShape ctShape = ctGroup.addNewShape();
ctShape.setStyle("width:100pt;height:24pt");
CTTxbxContent ctTxbxContent = ctShape.addNewTextbox().addNewTxbxContent();
ctTxbxContent.addNewP().addNewR().addNewT().setStringValue("The TextBox text...");
Node ctGroupNode = ctGroup.getDomNode();
CTPicture ctPicture = CTPicture.Factory.parse(ctGroupNode);
run=paragraph.createRun();
CTR cTR = run.getCTR();
cTR.addNewPict();
cTR.setPictArray(0, ctPicture);
简单展示下效果:
目录
目录本来就一句话doc.createTOC(),但是很容易失败。使用CTPPr可以设置段落的大纲级别,以下是代码:
try (XWPFDocument document = new XWPFDocument()) {
document.createTOC();
final XWPFParagraph paragraph = document.createParagraph();
// 获取段落属性,若不存在则新建
CTPPr ppr = paragraph.getCTP().isSetPPr() ? paragraph.getCTP().getPPr() : paragraph.getCTP().addNewPPr();
// 设置大纲级别为 1
CTDecimalNumber outlineLvl = ppr.isSetOutlineLvl() ? ppr.getOutlineLvl() : ppr.addNewOutlineLvl();
outlineLvl.setVal(BigInteger.valueOf(1));
final XWPFRun run = paragraph.createRun();
run.setText("标题一");
run.setFontSize(10);
run.setFontFamily("宋书");
File file = new File("toc.docx");
document.write(Files.newOutputStream(file.toPath()));
}
虽然代码运行不报错,但是结果是生成不了目录。以下是效果图:
图表
英文叫chart,chart是需要关联excel表格的。所以这个特别复杂。完整代码如下:
// create the data
String[] categories = new String[] { "Lang 1", "Lang 2", "Lang 3" };
Double[] valuesA = new Double[] { 10d, 20d, 30d };
Double[] valuesB = new Double[] { 15d, 25d, 35d };
// create the chart
XWPFChart chart = doc.createChart(15 * Units.EMU_PER_CENTIMETER, 10 * Units.EMU_PER_CENTIMETER);
// create data sources
int numOfPoints =categories. Length;
String categoryDataRange = chart.formatRange(new CellRangeAddress(1, numOfPoints, 0, 0));
String valuesDataRangeA = chart.formatRange(new CellRangeAddress(1, numOfPoints, 1, 1));
String valuesDataRangeB = chart.formatRange(new CellRangeAddress(1, numOfPoints, 2, 2));
XDDFDataSource<String> categoriesData = XDDFDataSourcesFactory.fromArray(categories, categoryDataRange, 0);
XDDFNumericalDataSource<Double> valuesDataA = XDDFDataSourcesFactory.fromArray(valuesA, valuesDataRangeA, 1);
XDDFNumericalDataSource<Double> valuesDataB = XDDFDataSourcesFactory.fromArray(valuesB, valuesDataRangeB, 2);
// create axis
XDDFCategoryAxis bottomAxis = chart.createCategoryAxis(AxisPosition.BOTTOM);
XDDFValueAxis leftAxis = chart.createValueAxis(AxisPosition.LEFT);
leftAxis.setCrosses(AxisCrosses.AUTO_ZERO);
// Set AxisCrossBetween, so the left axis crosses the category axis between the categories.
// Else first and last category is exactly on cross points and the bars are only half visible.
leftAxis.setCrossBetween(AxisCrossBetween.BETWEEN);
// create chart data
XDDFChartData data = chart.createData(ChartTypes.BAR, bottomAxis, leftAxis);
((XDDFBarChartData) data).setBarDirection(BarDirection.COL);
// create series
// if only one series do not vary colors for each bar
((XDDFBarChartData) data).setVaryColors(false);
XDDFChartData.Series series = data.addSeries(categoriesData, valuesDataA);
// XDDFChart.setSheetTitle is buggy. It creates a Table but only half way and incomplete.
// Excel cannot opening the workbook after creatingg that incomplete Table.
// So updating the chart data in Word is not possible.
//series.setTitle("a", chart.setSheetTitle("a", 1));
series.setTitle("a", setTitleInDataSheet(chart, "a", 1));
/*
// if more than one series do vary colors of the series
((XDDFBarChartData)data).setVaryColors(true);
series = data.addSeries(categoriesData, valuesDataB);
//series.setTitle("b", chart.setSheetTitle("b", 2));
series.setTitle("b", setTitleInDataSheet(chart, "b", 2));
*/
// plot chart data
chart.plot(data);
// create legend
XDDFChartLegend legend = chart.getOrAddLegend();
legend.setPosition(LegendPosition.LEFT);
legend.setOverlay(false);
还有一个私有方法:
static CellReference setTitleInDataSheet(XWPFChart chart, String title, int column) throws Exception {
XSSFWorkbook workbook = chart.getWorkbook();
XSSFSheet sheet = workbook.getSheetAt(0);
XSSFRow row = sheet.getRow(0);
if (row == null)
row = sheet.createRow(0);
XSSFCell cell = row.getCell(column);
if (cell == null)
cell = row.createCell(column);
cell.setCellValue(title);
return new CellReference(sheet.getSheetName(), 0, column, true, true);
}
运行效果如下: