POI reads Word (Word 2003 and Word 2007)

Source: Internet
Author: User

Recently in the system to the customer, the user asked for the need to be able to import Word files, now Microsoft Word has several versions 97, 2003, 2007, the three versions of the format of data storage is quite different, and now 97 has basically exited the market, Few people use this version, so in our system only consider 2003 version and 2007 version, because we only need to be able to read word text content, where the text style, pictures and other information can be ignored, and do not directly manipulate word files, so we choose to use Apache POI for reading.

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).

Before we write the code we need to download the required jar packages, we only need to download poi-bin-3.5-beta6-20090622.tar.gz and Openxml4j-bin-beta.jar, because the additional jar packages required can be poi-bin-3 Found in. 5-beta6-20090622.tar.gz, the following is: poi-bin-3.5-beta6-20090622.tar.gz:http://apache.etoak.com/poi/dev/bin/ Poi-bin-3.5-beta6-20090622.tar.gz openxml4j-bin-beta.jar:http://mirror.optus.net/sourceforge/o/op/openxml4j/ Below the Openxml4j-bin-beta.jar is the Java code that reads the word file, and it's worth noting that the POI does not read the picture information in the Word file when it reads the word file, and that it is for version 2007 of Word (. doc x), 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.

[Java]View Plaincopyprint?
  1. Import Java.io.File;
  2. Import Java.io.FileInputStream;
  3. Import Java.io.InputStream;
  4. Import org.apache.poi.POIXMLDocument;
  5. Import Org.apache.poi.POIXMLTextExtractor;
  6. Import Org.apache.poi.hwpf.extractor.WordExtractor;
  7. Import Org.apache.poi.openxml4j.opc.OPCPackage;
  8. Import Org.apache.poi.xwpf.extractor.XWPFWordExtractor;
  9. /**
  10. * POI reads the test class for text content in Word 2003 and Word 2007 <br/>
  11. * @createDate 2009-07-25
  12. * @author Carl He
  13. */
  14. Public class Test {
  15. public static void Main (string[] args) {
  16. try {
  17. //word 2003: Pictures are not read
  18. InputStream is = new FileInputStream (new File ("C://files//2003.doc"));
  19. Wordextractor ex = new Wordextractor (IS);
  20. String text2003 = Ex.gettext ();
  21. System.out.println (text2003);
  22. //word 2007 images are not read, the data in the table is placed at the end of the string
  23. Opcpackage opcpackage = poixmldocument.openpackage ("C://files//2007.docx");
  24. Poixmltextextractor extractor = new Xwpfwordextractor (opcpackage);
  25. String text2007 = Extractor.gettext ();
  26. System.out.println (text2007);
  27. } catch (Exception e) {
  28. E.printstacktrace ();
  29. }
  30. }
  31. }

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;/** * POI Read word Test classes for text content in 2003 and Word 2007 <br/> * @createDate 2009-07-25 * @author Carl He */public class Test {public static V OID Main (string[] args) {try {//word 2003: Picture is not read InputStream is = new FileInputStream (n EW File ("C://files//2003.doc")); Wordextractor ex = new Wordextractor (IS); String text2003 = Ex.gettext (); System.out.println (text2003); The word 2007 picture is not read, the data in the table is placed at the end of the string opcpackage opcpackage = Poixmldocument.openpackage ("C://files//2007.docx" ); Poixmltextextractor extractor = new Xwpfwordextractor (opcpackage); String text2007 = Extractor.getteXT (); System.out.println (text2007); } catch (Exception e) {e.printstacktrace (); } }}If you want to download the complete sample code, you can download it here, there are POI in this RAR package to read all the jar packages and Word 2003, Word 2007 sample files required by Word 2003 and Word 2007.

POI Read Word (Word 2003 and Word 2007) (GO)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.