Apache的POI是一种流行的API,允许程序员创建,修改和使用显示Java程序的MS-Office文件。它是开发和Apache软件基金会的分布式设计或修改使用Java程序MS-Office文件的开源库。它包含的类和方法来将用户输入的数据或文件到MS-Office文档解码。POI对ExcelWordPowerPoint甚至是Visio都有组件。近期工作中有项目需要生成Word,并且有指定样式(字体、字号页眉页脚、水印等),特记录一下POI在Word上的用法。

官网地址:https://poi.apache.org/

1. 创建docx文档(写入段落和表格)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public static void main(String[] args) throws XmlException, IOException
{
//创建一个空白文档
XWPFDocument document = new XWPFDocument();
//给段落加下边框
paragraph.setBorderBottom(Borders.BASIC_BLACK_DASHES);
//给段落加左边框
paragraph.setBorderLeft(Borders.BASIC_BLACK_DASHES);
//给段落加右边框
paragraph.setBorderRight(Borders.BASIC_BLACK_DASHES);
//给段落加上边框
paragraph.setBorderTop(Borders.BASIC_BLACK_DASHES);
//创建一个段落
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("这是一个word文档,用于测试,用poi写入。 可以有空格,可以改写");
//创建表格
XWPFTable table = document.createTable();
//创建第一行
XWPFTableRow tableRowOne = table.getRow(0);
tableRowOne.getCell(0).setText("col one, row one");
tableRowOne.addNewTableCell().setText("col two, row one");
tableRowOne.addNewTableCell().setText("col three, row one");
//创建第二行
XWPFTableRow tableRowTwo = table.createRow();
tableRowTwo.getCell(0).setText("col one, row two");
tableRowTwo.getCell(1).setText("col two, row two");
tableRowTwo.getCell(2).setText("col three, row two");
//Write the Document in file system
FileOutputStream out = new FileOutputStream(new File("C:\\Users\\dell\\Desktop\\createparagraph.docx"));
document.write(out);
out.close();
}

2. 段落对齐和添加样式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
public static void main(String[] args) throws XmlException, IOException
{
XWPFDocument document = new XWPFDocument();
//创建段落
XWPFParagraph paragraphone = document.createParagraph();
//设置段落右对齐样式
paragraphone.setAlignment(ParagraphAlignment.RIGHT);
XWPFRun runone=paragraphone.createRun();
runone.setText("右对齐右对齐右对齐右对齐右对齐右对齐右对齐右对齐右对齐右对齐.");
//创建一个新段落
XWPFParagraph paragraphtwo=document.createParagraph();
//设置段落居中对齐样式
paragraphtwo.setAlignment(ParagraphAlignment.CENTER);
XWPFRun runtwo=paragraphtwo.createRun();
runtwo.setText("居中对齐居中对齐居中对齐居中对齐居中对齐居中对齐居中对齐居中对齐.");
//创建一个新段落
XWPFParagraph paragraphthree = document.createParagraph();
//设置加粗和斜体
XWPFRun paragraphOneRunOne = paragraphthree.createRun();
paragraphOneRunOne.setBold(true);
paragraphOneRunOne.setItalic(true);
paragraphOneRunOne.setText("Font Style");
paragraphOneRunOne.addBreak();
//设置文本位置
XWPFRun paragraphOneRunTwo = paragraphthree.createRun();
paragraphOneRunTwo.setText("Font Style two");
paragraphOneRunTwo.setTextPosition(100);
//创建一个新段落
XWPFRun paragraphOneRunThree = paragraphthree.createRun();
//设置文字中划线样式(可能弃用)
paragraphOneRunThree.setStrike(true);
//设置文字大小
paragraphOneRunThree.setFontSize(20);
//设置文字段落内对齐,垂直对齐(垂直靠下对齐,垂直居中对齐,垂直向上对齐)
paragraphOneRunThree.setSubscript(VerticalAlign.SUBSCRIPT);
paragraphOneRunThree.setText(" Different Font Styles");
//Write the Document in file system
FileOutputStream out = new FileOutputStream(newFile("C:\\Users\\dell\\Desktop\\createparagraph.docx"));
document.write(out);
out.close();
}

3. 从已有的docx文件中获取信息

1
2
3
4
5
6
7
8
public static void main(String[] args) throws XmlException, IOException
{
//传流构建XWPFDocument
XWPFDocument docx = new XWPFDocument(new FileInputStream("C:\\Users\\dell\\Desktop\\createparagraph.docx"));
//传路径构建XWPFDocument
XWPFDocument document = new XWPFDocument(POIXMLDocument.openPackage("C:\\Users\\dell\\Desktop\\createparagraph.docx"));
}

4. 处理页眉页脚(由模板文件进行文字替换)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
public static void replaceFooterAndHeader(XWPFDocument doc){
List<XWPFParagraph> footers = doc.getHeaderFooterPolicy().getDefaultFooter().getParagraphs();
List<XWPFParagraph> headers = doc.getHeaderFooterPolicy().getDefaultHeader().getParagraphs();
//处理页脚
for (XWPFParagraph paragraph : footers) {
List<XWPFRun> runs = paragraph.getRuns();
for (XWPFRun run : runs) {
String text = run.getText(0);
if(StringUtils.isNotEmpty(text)){
for(Entry<String, String> entry : params.entrySet()){
String key = entry.getKey();
if(text.indexOf(key) != -1){
Object value = entry.getValue();
if(value instanceof String){
text = text.replace(key, value.toString());
run.setText(text,0);
}
}
}
}
}
}
//处理页眉
for (XWPFParagraph paragraph : headers) {
List<XWPFRun> runs = paragraph.getRuns();
for (XWPFRun run : runs) {
String text = run.getText(0);
if(StringUtils.isNotEmpty(text)){
for(Entry<String, String> entry : params.entrySet()){
String key = entry.getKey();
if(text.indexOf(key) != -1){
Object value = entry.getValue();
if(value instanceof String){
text = text.replace(key, value.toString());
run.setText(text,0);
}
}
}
}
}
}
}

5.获取docx文件中的图片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public static void main(String[] args) throws XmlException, InvalidFormatException, FileNotFoundException, IOException{
String path ="C:\\Users\\dell\\Desktop\\picture.docx";
File file = new File(path);
FileInputStream fis = new FileInputStream(file);
XWPFDocument document = new XWPFDocument(fis);
XWPFWordExtractor xwpfWordExtractor = new XWPFWordExtractor(document);
String text = xwpfWordExtractor.getText();
System.out.println(text);
List<XWPFPictureData> picList = document.getAllPictures();
for (XWPFPictureData pic : picList) {
System.out.println(pic.getPictureType() + file.separator + pic.suggestFileExtension()+file.separator+pic.getFileName());
byte[] bytev = pic.getData();
FileOutputStream fos = new FileOutputStream("d:\\"+pic.getFileName());
fos.write(bytev);
}
fis.close();
}

6. POI读取.doc 和.docx

6.1 HWPF和XWPF

HWPF:MS-Word 97-2003(.doc),基于BIFF8格式的JAVA接口。只支持.doc文件简单的操作,读写能力有限。本API为POI项目早期开发,很不幸的 是主要负责HWPF模块开发的工程师已经离开Apache组织,现在该模块没有人维护、更新、完善。

XWPF:MS-Word 2007+(.docx),基于OOXML格式的JAVA接口。较HWPF功能完善。

6.2 HSSF和XSSF

HSSF:MS-Excel 97-2003(.xls),基于BIFF8格式的JAVA接口。

XSSF:MS-Excel 2007+(.xlsx),基于OOXML格式的JAVA接口。

具体见: POI读取.doc 和.docx的区别

另外很好的例子地址:http://www.cnblogs.com/unruly/p/7552998.html