如何从特定表java apache POI中提取文本

时间:2016-04-14 14:25:53

标签: java apache-poi

我有一个包含三个表的.doc文档。每个表略有不同,因此必须以不同方式提取文本。我试图使用apache POI库来做这个我想用不同的代码处理每个表但我无法弄清楚如何做到这一点因为似乎没有tableIndex作为apache的一部分POI。目前我对它们的处理与下面相同,但有人知道如何指定特定的表:

import java.io.*;
import java.util.*;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.model.*;
import org.apache.poi.hwpf.usermodel.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class Main {

    public static void main (String[] args) throws Exception {
        String fileName = "MyFile.doc";
        InputStream fis = new FileInputStream(fileName);  
        POIFSFileSystem fs = new POIFSFileSystem(fis);  
        HWPFDocument doc = new HWPFDocument(fs);  

        Range range = doc.getRange();
        Paragraph para = range.getParagraph(0);

        String str="";

        TableIterator itr = new TableIterator(range);
        while(itr.hasNext()){
            Table table = itr.next();
            for(int rowIndex = 0; rowIndex < table.numRows(); rowIndex++){
                TableRow row = table.getRow(rowIndex);
                for(int colIndex = 0; colIndex < 1; colIndex++){
                    //Getting the first two cell text values out of the table
                    TableCell cell0 = row.getCell(0);
                    TableCell cell1 = row.getCell(1);
                    str= cell0.text()+" : "+cell1.text()+" : ";
                    System.out.println(str);
                }
            }
        }
    }
}

0 个答案:

没有答案