HWPFDocument中的要素
Range 表示一个范围( 内容:Section,Paragraph,CharacterRun)
Section 文档中的一个小节,文档由多个小节组成(内容:Paragraph)
Paragraph 段落(内容:CharacterRun)
CharacterRun 具有相同属性的文本
Table 表格
TableRow 表格对应的行
TableCell 表格对应的单元格
***Section、Paragraph、CharacterRun和Table都继承自Range***
maven引用
< dependency>
< groupId> org.apache.poi</ groupId>
< artifactId> poi-scratchpad</ artifactId>
< version> 3.17</ version>
</ dependency>
< dependency>
< groupId> commons-io</ groupId>
< artifactId> commons-io</ artifactId>
< version> 2.0</ version>
</ dependency>
< dependency>
< groupId> commons-lang</ groupId>
< artifactId> commons-lang</ artifactId>
< version> 2.0</ version>
</ dependency>
通过WordExtractor读取文本
InputStream is = new FileInputStream ( "d://04.doc" ) ;
WordExtractor extractor = new WordExtractor ( is) ;
String text = extractor. getText ( ) ;
String textFromPieces = extractor. getTextFromPieces ( ) ;
private void printBookmarks ( Bookmarks bookmarks) {
int count = bookmarks. getBookmarksCount ( ) ;
System. out. println ( "书签数量:" + count) ;
Bookmark bookmark;
for ( int i = 0 ; i < count; i++ ) {
bookmark = bookmarks. getBookmark ( i) ;
System. out. println ( "书签" + ( i + 1 ) + "的名称是:" + bookmark. getName ( ) ) ;
System. out. println ( "开始位置:" + bookmark. getStart ( ) ) ;
System. out. println ( "结束位置:" + bookmark. getEnd ( ) ) ;
}
}
private void printSummary ( SummaryInformation info) {
System. out. println ( info. getAuthor ( ) ) ;
System. out. println ( info. getCharCount ( ) ) ;
System. out. println ( info. getPageCount ( ) ) ;
System. out. println ( info. getTitle ( ) ) ;
System. out. println ( info. getSubject ( ) ) ;
}
通过HWPFDocument读文件
InputStream is = new FileInputStream ( "d://04.doc" ) ;
HWPFDocument hwpfDocument = new HWPFDocument ( is) ;
String text1 = hwpfDocument. getDocumentText ( ) ;
System. err. println ( text1) ;
Range range = hwpfDocument. getRange ( ) ;
String text2 = range. text ( ) ;
System. err. println ( text2) ;
private void printParagraph ( Range range) {
int num = range. numParagraphs ( ) ;
for ( int i = 0 ; i < num; i++ ) {
Paragraph para = range. getParagraph ( i) ;
if ( para. isInList ( ) ) {
System. out. println ( "list: " + para. text ( ) ) ;
}
}
}
private void printSection ( Range range) {
int secNum = range. numSections ( ) ;
System. out. println ( secNum) ;
for ( int i = 0 ; i < secNum; i++ ) {
Section section = range. getSection ( i) ;
System. out. println ( section. getMarginLeft ( ) ) ;
System. out. println ( section. getMarginRight ( ) ) ;
System. out. println ( section. getMarginTop ( ) ) ;
System. out. println ( section. getMarginBottom ( ) ) ;
System. out. println ( section. getPageHeight ( ) ) ;
System. out. println ( section. text ( ) ) ;
}
}
通过HWPFDocument写文件
public static void main ( String[ ] args) throws IOException {
String wordMouldFile = "d://tmp//module.doc" ;
Map< String, String> replaceMap = new HashMap < String, String> ( ) ;
String outputFile = "d://tmp//导出.doc" ;
replaceMap. put ( "title1" , "标题一" ) ;
replaceMap. put ( "content1" , "内容一" ) ;
replaceMap. put ( "title2" , "标题二" ) ;
replaceMap. put ( "content2" , "内容二" ) ;
HWPFDocumentUtil hwpfDocumentUtil = new HWPFDocumentUtil ( wordMouldFile, replaceMap) ;
hwpfDocumentUtil. exportWord ( new File ( outputFile) ) ;
String readWordText = HWPFDocumentUtil. readWordText ( outputFile) ;
System. err. println ( readWordText) ;
}
public class HWPFDocumentUtil {
private String wordMouldFile;
private Map< String, String> replaceMap;
public HWPFDocumentUtil ( String wordMouldFile, Map< String, String> replaceMap) {
super ( ) ;
this . wordMouldFile = wordMouldFile;
this . replaceMap = replaceMap;
}
public static String readWordText ( String filename) throws IOException {
if ( StringUtils. isBlank ( filename) ) {
return "" ;
}
File file = new File ( filename) ;
if ( ! file. exists ( ) ) {
throw new FileNotFoundException ( filename) ;
}
InputStream is = null;
HWPFDocument hwpfDocument = null;
try {
is = new FileInputStream ( filename) ;
hwpfDocument = new HWPFDocument ( is) ;
Range range = hwpfDocument. getRange ( ) ;
String text = range. text ( ) ;
return StringUtils. trimToEmpty ( text) ;
} catch ( IOException e) {
throw e;
} finally {
IOUtils. closeQuietly ( is) ;
hwpfDocument. close ( ) ;
hwpfDocument= null;
}
}
public void exportWord ( File outFile) {
InputStream is = null;
HWPFDocument doc = null;
OutputStream os = null;
try {
is = new FileInputStream ( wordMouldFile) ;
doc = new HWPFDocument ( is) ;
Range range = doc. getRange ( ) ;
Set< String> keySet = replaceMap. keySet ( ) ;
for ( String key : keySet) {
String value = replaceMap. get ( key) ;
range. replaceText ( "$" + key, value) ;
}
os = new FileOutputStream ( outFile) ;
doc. write ( os) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
} finally {
try {
doc. close ( ) ;
doc = null;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
IOUtils. closeQuietly ( is) ;
IOUtils. closeQuietly ( os) ;
}
}
. . . [ get set 忽略]
}