The Apache POI is an open source library of the Apache Software Foundation, and POI provides the ability for the API to read and write to Microsoft Office format files in Java programs.
1. Read the jar packs that Word 2003 and Word 2007 need
A Word file that reads Version 2003 (. doc) is relatively simple, requiring only Poi-3.5-beta6-20090622.jar and Poi-scratchpad-3.5-beta6-20090622.jar two jar packages, and 2007 version (. docx) on more trouble, I said this trouble is not the time we write code trouble, is to import more jar package, there are as many as the following 7:
1. Openxml4j-bin-beta.jar
2. Poi-3.5-beta6-20090622.jar
3. Poi-ooxml-3.5-beta6-20090622.jar
4. Dom4j-1.6.1.jar
5. Geronimo-stax-api_1.0_spec-1.0.jar
6. Ooxml-schemas-1.0.jar
7. Xmlbeans-2.3.0.jar
4-7 of these are the jar packages that Poi-ooxml-3.5-beta6-20090622.jar relies on (found in the Ooxml-lib directory in poi-bin-3.5-beta6-20090622.tar.gz).
2. Line Change symbol
Hard Wrap: wrapping in a file, if the line is used in the keyboard for "enter".
Soft Wrap: The number of characters in a line in a file is limited, and when the number of characters exceeds a certain value, it is automatically cut to the downward display.
For the program, the hard line is recognized, the determination of the line-wrapping, soft line and font size, indentation related.
3. Reading considerations
It's worth noting that POI does not read picture information in Word files , and that for version 2007 of Word (. docx), if there are tables in the Word file, the data in all tables will be at the end of the read string.
4. Read Word text content code
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.InputStream;
Import org.apache.poi.POIXMLDocument;
Import Org.apache.poi.POIXMLTextExtractor;
Import Org.apache.poi.hwpf.extractor.WordExtractor;
Import Org.apache.poi.openxml4j.opc.OPCPackage;
Import Org.apache.poi.xwpf.extractor.XWPFWordExtractor;
public class Test {public
static void Main (string[] args) {
try {
InputStream is = new FileInputStream (new File ("2003.doc"));
Wordextractor ex = new Wordextractor (is);
String text2003 = Ex.gettext ();
System.out.println (text2003);
Opcpackage opcpackage = poixmldocument.openpackage ("2007.docx");
Poixmltextextractor extractor = new Xwpfwordextractor (opcpackage);
String text2007 = Extractor.gettext ();
System.out.println (text2007);
} catch (Exception e) {
e.printstacktrace ();}}}