POI讀取word文件的表格數據

jopen 10年前發布 | 65K 次閱讀 POI Office文檔處理

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();
}
}
}</pre>

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!