Java Read Word file

Source: Internet
Author: User
Tags gettext

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

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.