Java POI Read Word file
Apache POI is an open source library of the Apache Software Foundation, where POI provides the ability for Java programs to read and write to Microsoft Office format files.
1. Read the jar packages required for Word 2003 and Word 2007
Word files that read version 2003 (. doc) are relatively simple and require only Poi-3.5-beta6-20090622.jar and Poi-scratchpad-3.5-beta6-20090622.jar two jar packages, while 2007 version (. docx) on the trouble, I said this trouble is not when we write code trouble, is to import the jar package compared more, there are 7 of the following: 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 is the jar package that Poi-ooxml-3.5-beta6-20090622.jar relies on (Ooxml-lib in poi-bin-3.5-beta6-20090622.tar.gz Directory can be found).
2. Line break symbols
Hard Wrap: wraps the file, if it is a line break that uses "enter" in the keyboard.
Soft Wrap: The number of characters in a single line of a file is limited, and when the number of characters exceeds a certain value, it is automatically cut to the downstream display .
For the program, the hard line break is a recognizable, determined line-break, soft-line with the font size, indentation.
3. Considerations for Reading
It is worth noting that the POI does not read the picture information in the Word file , and that for the 2007 version of Word (. docx), if there is a table in the Word file, all the data in the table will be at the end of the read-out string.
4. Read the Word text content code
1ImportJava.io.File;2ImportJava.io.FileInputStream;3ImportJava.io.InputStream;45ImportOrg.apache.poi.POIXMLDocument;6ImportOrg.apache.poi.POIXMLTextExtractor;7ImportOrg.apache.poi.hwpf.extractor.WordExtractor;8ImportOrg.apache.poi.openxml4j.opc.OPCPackage;9ImportOrg.apache.poi.xwpf.extractor.XWPFWordExtractor;1011PublicClassTest {12PublicStaticvoidMain (string[] args) {13Try{InputStream is =New FileInputStream (New File ("2003.doc"));Wordextractor ex =NewWordextractor (IS);String text2003 =Ex.gettext ();17System.out.println (text2003);18Opcpackage opcpackage = poixmldocument.openpackage ("2007.docx"); poixmltextextractor extractor = new xwpfwordextractor (opcpackage); String text2007 = extractor.gettext (); System.out.println (text2007); (Exception e) { e.printstacktrace (); +}
Java Read Word file