Poi3.8 read the table data of the Word document!

Source: Internet
Author: User
Package CN. CCB. boup. util. poi; import Java. io. fileinputstream; import Java. io. ioexception; import Java. util. arraylist; import Java. util. iterator; import Java. util. list; import Org. apache. poi. hwpf. hwpfdocument; import Org. apache. poi. hwpf. usermodel. paragraph; import Org. apache. poi. hwpf. usermodel. range; import Org. apache. poi. hwpf. usermodel. table; import Org. apache. poi. hwpf. usermodel. tablecell; import Org. apache. P Oi. hwpf. usermodel. tableiterator; import Org. apache. poi. hwpf. usermodel. tablerow; import Org. apache. poi. poifs. filesystem. poifsfilesystem; /***** @ author guojigang **/public class operateword {/*** read the table data of a Word document ** @ Param filepath * @ return list */public static list readwordcell (string filepath) {fileinputstream in = NULL; poifsfilesystem PFS = NULL; List <string> List = new arraylist <string> (); try {in = New fileinputstream (filepath); // load the document PFS = new poifsfilesystem (in); hwpfdocument hwpf = new hwpfdocument (PFS); range = hwpf. getrange (); // get the reading range of the document. tableiterator it = new tableiterator (range); // iterate the table if (it. hasnext () {tablerow TR = NULL; tablecell TD = NULL; paragraph para = NULL; string linestring; string cellstring; Table TB = (table) it. next (); // iteration row, starting from row 2nd for (INT I = 2; I <TB. numrows (); I ++) {TR = TB. getrow (I); linestring = ""; for (Int J = 0; j <tr. numcells (); j ++) {TD = tr. getcell (j); // obtain the cell content. // obtain the cell content for (int K = 0; k <TD. numparagraphs (); k ++) {para = TD. getparagraph (k); cellstring = para. text (); If (cellstring! = NULL & cellstring. compareto ("")! = 0) {// If trim is not performed, the extracted content will contain a garbled character cellstring = cellstring. trim () + "|" ;}linestring + = cellstring ;}// remove a pipe character at the end of the string if (linestring! = NULL & linestring. compareto ("")! = 0) {linestring = linestring. substring (0, linestring. length ()-1);} List. add (linestring) ;}} catch (exception e) {e. printstacktrace ();} finally {If (null! = In) {try {In. close ();} catch (ioexception e) {e. printstacktrace () ;}}return list;} public static void main (string [] ARGs) {list = operateword. readwordcell ("C: \ id = fruit .doc"); For (iterator iter = List. iterator (); ITER. hasnext ();) {string STR = (string) ITER. next (); system. err. println (STR );}}}

Required class libraries:

Poi-3.8-beta5-20111217.jar
Poi-scratchpad-3.8-beta5-20111217.jar

Download from http://poi.apache.org.

The test is normal, but in the project, the following error will be reported, which should be caused by class conflicts.

Exception in thread "Main" Java. Lang. nosuchmethoderror: org. Apache. Poi. poidocument. <init> (lorg/Apache/poi/poifs/filesystem/directorynode;) V
At org. Apache. Poi. hwpf. hwpfdocumentcore. <init> (hwpfdocumentcore. Java: 146)
At org. Apache. Poi. hwpf. hwpfdocument. <init> (hwpfdocument. Java: 218)
At org. Apache. Poi. hwpf. hwpfdocument. <init> (hwpfdocument. Java: 186)
At CN. CCB. boup. util. Poi. operateword. readwordcell (operateword. Java: 37)
At CN. CCB. boup. util. Poi. operateword. Main (operateword. Java: 87)

Solution: Open properties-> JAVA build path-> order and export of the project, find that the two jar packages of POI are put to the end, and move the poi package to the top. Solve the problem!

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.