读word doc文件2中方式
1.1 通过WordExtractor读文件(在WordExtractor内部进行信息读取时还是通过HWPFDocument来获取的。)
1.2 通过HWPFDocument读文件
Apache poi的hwpf模块是专门用来对word doc文件进行读写操作的。在hwpf里面我们使用HWPFDocument来表示一个word doc文档。在HWPFDocument里面有这么几个概念:
l Range:它表示一个范围,这个范围可以是整个文档,也可以是里面的某一小节(Section),也可以是某一个段落(Paragraph),还可以是拥有共同属性的一段文本(CharacterRun)。
l Section:word文档的一个小节,一个word文档可以由多个小节构成。
l Paragraph:word文档的一个段落,一个小节可以由多个段落构成。
l CharacterRun:具有相同属性的一段文本,一个段落可以由多个CharacterRun组成。
l Table:一个表格。
l TableRow:表格对应的行。
l TableCell:表格对应的单元格。
Section、Paragraph、CharacterRun和Table都继承自Range。
1.1 通过WordExtractor读文件
在使用WordExtractor读文件时我们只能读到文件的文本内容和基于文档的一些属性,至于文档内容的属性等是无法读到的。如果要读到文档内容的属性则需要使用HWPFDocument来读取了。下面是使用WordExtractor读取文件的一个示例:
1. public class HwpfTest {
2.
3. @SuppressWarnings("deprecation")
4. @Test
5. public void testReadByExtractor() throws Exception {
6. new FileInputStream("D:\\test.doc");
7. new WordExtractor(is);
8. //输出word文档所有的文本
9. System.out.println(extractor.getText());
10. System.out.println(extractor.getTextFromPieces());
11. //输出页眉的内容
12. "页眉:" + extractor.getHeaderText());
13. //输出页脚的内容
14. "页脚:" + extractor.getFooterText());
15. //输出当前word文档的元数据信息,包括作者、文档的修改时间等。
16. System.out.println(extractor.getMetadataTextExtractor().getText());
17. //获取各个段落的文本
18. String paraTexts[] = extractor.getParagraphText();
19. for (int i=0; i<paraTexts.length; i++) {
20. "Paragraph " + (i+1) + " : " + paraTexts[i]);
21. }
22. //输出当前word的一些信息
23. printInfo(extractor.getSummaryInformation());
24. //输出当前word的一些信息
25. this.printInfo(extractor.getDocSummaryInformation());
26. this.closeStream(is);
27. }
28.
29. /**
30. * 输出SummaryInfomation
31. * @param info
32. */
33. private void printInfo(SummaryInformation info) {
34. //作者
35. System.out.println(info.getAuthor());
36. //字符统计
37. System.out.println(info.getCharCount());
38. //页数
39. System.out.println(info.getPageCount());
40. //标题
41. System.out.println(info.getTitle());
42. //主题
43. System.out.println(info.getSubject());
44. }
45.
46. /**
47. * 输出DocumentSummaryInfomation
48. * @param info
49. */
50. private void printInfo(DocumentSummaryInformation info) {
51. //分类
52. System.out.println(info.getCategory());
53. //公司
54. System.out.println(info.getCompany());
55. }
56.
57. /**
58. * 关闭输入流
59. * @param is
60. */
61. private void closeStream(InputStream is) {
62. if (is != null) {
63. try {
64. is.close();
65. catch (IOException e) {
66. e.printStackTrace();
67. }
68. }
69. }
70.
71. }
1.2 通过HWPFDocument读文件
HWPFDocument
是当前
Word
文档的代表,它的功能比
WordExtractor
要强。通过它我们可以读取文档中的表格、列表等,还可以对文档的内容进行新增、修改和删除操作。只是在进行完这些新增、修改和删除后相关信息是保存在
HWPFDocument
中的,也就是说我们改变的是
HWPFDocument
,而不是磁盘上的文件。如果要使这些修改生效的话,我们可以调用
HWPFDocument
的
write
方法把修改后的
HWPFDocument
输出到指定的输出流中。这可以是原文件的输出流,也可以是新文件的输出流(相当于另存为)或其它输出流。下面是一个通过
HWPFDocument
读文件的示例:
1. public class HwpfTest {
2.
3. @Test
4. public void testReadByDoc() throws Exception {
5. new FileInputStream("D:\\test.doc");
6. new HWPFDocument(is);
7. //输出书签信息
8. this.printInfo(doc.getBookmarks());
9. //输出文本
10. System.out.println(doc.getDocumentText());
11. Range range = doc.getRange();
12. //this.insertInfo(range);
13. this.printInfo(range);
14. //读表格
15. this.readTable(range);
16. //读列表
17. this.readList(range);
18. //删除range
19. new Range(2, 5, doc);
20. //在内存中进行删除,如果需要保存到文件中需要再把它写回文件
21. //把当前HWPFDocument写到输出流中
22. new FileOutputStream("D:\\test.doc"));
23. this.closeStream(is);
24. }
25.
26. /**
27. * 关闭输入流
28. * @param is
29. */
30. private void closeStream(InputStream is) {
31. if (is != null) {
32. try {
33. is.close();
34. catch (IOException e) {
35. e.printStackTrace();
36. }
37. }
38. }
39.
40. /**
41. * 输出书签信息
42. * @param bookmarks
43. */
44. private void printInfo(Bookmarks bookmarks) {
45. int count = bookmarks.getBookmarksCount();
46. "书签数量:" + count);
47. Bookmark bookmark;
48. for (int i=0; i<count; i++) {
49. bookmark = bookmarks.getBookmark(i);
50. "书签" + (i+1) + "的名称是:" + bookmark.getName());
51. "开始位置:" + bookmark.getStart());
52. "结束位置:" + bookmark.getEnd());
53. }
54. }
55.
56. /**
57. * 读表格
58. * 每一个回车符代表一个段落,所以对于表格而言,每一个单元格至少包含一个段落,每行结束都是一个段落。
59. * @param range
60. */
61. private void readTable(Range range) {
62. //遍历range范围内的table。
63. new TableIterator(range);
64. Table table;
65. TableRow row;
66. TableCell cell;
67. while (tableIter.hasNext()) {
68. table = tableIter.next();
69. int rowNum = table.numRows();
70. for (int j=0; j<rowNum; j++) {
71. row = table.getRow(j);
72. int cellNum = row.numCells();
73. for (int k=0; k<cellNum; k++) {
74. cell = row.getCell(k);
75. //输出单元格的文本
76. System.out.println(cell.text().trim());
77. }
78. }
79. }
80. }
81.
82. /**
83. * 读列表
84. * @param range
85. */
86. private void readList(Range range) {
87. int num = range.numParagraphs();
88. Paragraph para;
89. for (int i=0; i<num; i++) {
90. para = range.getParagraph(i);
91. if (para.isInList()) {
92. "list: " + para.text());
93. }
94. }
95. }
96.
97. /**
98. * 输出Range
99. * @param range
100. */
101. private void printInfo(Range range) {
102. //获取段落数
103. int paraNum = range.numParagraphs();
104. System.out.println(paraNum);
105. for (int i=0; i<paraNum; i++) {
106. //this.insertInfo(range.getParagraph(i));
107. "段落" + (i+1) + ":" + range.getParagraph(i).text());
108. if (i == (paraNum-1)) {
109. this.insertInfo(range.getParagraph(i));
110. }
111. }
112. int secNum = range.numSections();
113. System.out.println(secNum);
114. Section section;
115. for (int i=0; i<secNum; i++) {
116. section = range.getSection(i);
117. System.out.println(section.getMarginLeft());
118. System.out.println(section.getMarginRight());
119. System.out.println(section.getMarginTop());
120. System.out.println(section.getMarginBottom());
121. System.out.println(section.getPageHeight());
122. System.out.println(section.text());
123. }
124. }
125.
126. /**
127. * 插入内容到Range,这里只会写到内存中
128. * @param range
129. */
130. private void insertInfo(Range range) {
131. "Hello");
132. }
133.
134. }
















