使用POI解析WORD,能解析到WORD中的表格信息吗
网上大多都是只解析出所有文本,却没取得表格信息在这里懦懦的问一下前辈们,能不能现实这个需求,还希望给个相关例子,谢谢

解决方案 »

  1.   

    希望这个可以帮你
    public String[] getAllText() {
    int numP = range.numParagraphs(); //得到range范围的Paragraph的个数
    int m = 0; //数组下标
    String[] ret = new String[numP];
    for (int i = 0; i < numP; ++i) {
    // 从每一段落中获取文字,每一段是一个回车
    Paragraph p = range.getParagraph(i);
    test = p.isInTable(); //判断该Paragraph是否在word的表格中
    if (test == true) {
    table = range.getTable(p); //通过第一个在table中的Paragraph来获取整个table
    int numRow = table.numRows(); //获取table中的行数 for (int j = 0; j < numRow; j++) {
    tablerow = table.getRow(j); //获得一行
    int numbercell = tablerow.numCells(); //通过tablerow获取单元格个数 for (int k = 0; k < numbercell; k++) { tablexell = tablerow.getCell(k); //获得单元格 int p1 = tablexell.numParagraphs();
    //获取单元格中的Paragraph的个数
    String str = "";
    for (int l = 0; l < p1; l++) { Paragraph para = tablexell.getParagraph(l);
    str = str + para.text().trim(); //得到单元格中的内容 }
    for (int n = m; n < numP;) {
    ret[n] = str; //将单元格中的内容方入数组元素中
    break;
    }
    m++;
    }
    }
    return ret;
    }
    } return ret;
    }
      

  2.   

    希望这个可以帮你
    public String[] getAllText() {
    int numP = range.numParagraphs(); //得到range范围的Paragraph的个数
    int m = 0; //数组下标
    String[] ret = new String[numP];
    for (int i = 0; i < numP; ++i) {
    // 从每一段落中获取文字,每一段是一个回车
    Paragraph p = range.getParagraph(i);
    test = p.isInTable(); //判断该Paragraph是否在word的表格中
    if (test == true) {
    table = range.getTable(p); //通过第一个在table中的Paragraph来获取整个table
    int numRow = table.numRows(); //获取table中的行数 for (int j = 0; j < numRow; j++) {
    tablerow = table.getRow(j); //获得一行
    int numbercell = tablerow.numCells(); //通过tablerow获取单元格个数 for (int k = 0; k < numbercell; k++) { tablexell = tablerow.getCell(k); //获得单元格 int p1 = tablexell.numParagraphs();
    //获取单元格中的Paragraph的个数
    String str = "";
    for (int l = 0; l < p1; l++) { Paragraph para = tablexell.getParagraph(l);
    str = str + para.text().trim(); //得到单元格中的内容 }
    for (int n = m; n < numP;) {
    ret[n] = str; //将单元格中的内容方入数组元素中
    break;
    }
    m++;
    }
    }
    return ret;
    }
    } return ret;
    }
      

  3.   


    public void testWord(String fileString,String newPath) {
    File file = new File(fileString);
    try {
    FileInputStream in = new FileInputStream(file);
    POIFSFileSystem pfs = new POIFSFileSystem(in);
    HWPFDocument hwpf = new HWPFDocument(pfs);
    Range range = hwpf.getRange();
    StyleSheet styleSheet = hwpf.getStyleSheet();
    TableIterator it = new TableIterator(range);
    // 遍历一个DOC中的所有表格
    while (it.hasNext()) {
    Table tb = (Table) it.next(); // 遍历表格的行
    for (int i = 0; i < tb.numRows(); i++) {
    TableRow tr = tb.getRow(i);
    // 遍历表格的列
    for (int j = 0; j < tr.numCells(); j++) {
    // 往表格中插入数据
    TableCell td = tr.getCell(j);
    String text = "第" + i + "行第" + j + "列";
    int p = td.numParagraphs();
    Paragraph para = td.getParagraph(p);
    ParagraphProperties pp = new ParagraphProperties();
    System.out.println(para.);
    //if(para.){
    para.insertBefore(text);
    //}
    }
    }
    }
    // 在表格外面插入内容
    CharacterProperties cp = new CharacterProperties();
    cp.setBold(true);
    cp.setCharacterSpacing(10);
    cp.setChse(cp.SPRM_CHARSCALE);
    cp.setCapitalized(true);
    //int p = range.numParagraphs();
    //Paragraph para = range.getParagraph(p);
    //para.insertAfter("test poi successful!", cp);
    //para.insertAfter("测试成功", cp);
    File outputFile = new File(newPath);
    OutputStream output = new FileOutputStream(outputFile);
    hwpf.write(output);
    output.close();

    } catch (Exception ex) {
    ex.printStackTrace();
    }
    这种可以,不过有点错误,要解决一下
      

  4.   

    怎样生成一个空白的word文档啊?