POI读取word文件的表格数据

jopen 10年前

20130725101029328.png

package com.poi.world;        import java.io.FileInputStream;        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.poi.hwpf.usermodel.TableIterator;    import org.apache.poi.hwpf.usermodel.TableRow;    import org.apache.poi.poifs.filesystem.POIFSFileSystem;        public class POI_Word{        public  static void main(String[] args){            try {                String[] s=new String[20];                FileInputStream in=new FileInputStream("D:\\mayi.doc");                POIFSFileSystem pfs=new POIFSFileSystem(in);                HWPFDocument hwpf=new HWPFDocument(pfs);                Range range =hwpf.getRange();                TableIterator it=new TableIterator(range);                int index=0;                while(it.hasNext()){                    Table tb=(Table)it.next();                    for(int i=0;i<tb.numRows();i++){                        //System.out.println("Numrows :"+tb.numRows());                        TableRow tr=tb.getRow(i);                        for(int j=0;j<tr.numCells();j++){                            //System.out.println("numCells :"+tr.numCells());    //                      System.out.println("j   :"+j);                            TableCell td=tr.getCell(j);                            for(int k=0;k<td.numParagraphs();k++){                                //System.out.println("numParagraphs :"+td.numParagraphs());                                Paragraph para=td.getParagraph(k);                                 s[index]=para.text().trim();                                 index++;                            }                        }                    }                }    //          System.out.println(s.toString());                for(int i=0;i<s.length;i++){                    System.out.println(s[i]);                }            } catch (Exception e) {                e.printStackTrace();            }        }    }