java 读取word文件

package com.example;


import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import java.io.FileInputStream;

public class wordPdf {

    public static void main(String[] args) {
        try{
            FileInputStream in = new FileInputStream("/Users/wangrongfei/Downloads/中国标准文献分类号CCS.doc");// 载入文档
            POIFSFileSystem pfs = new POIFSFileSystem(in);
            HWPFDocument hwpf = new HWPFDocument(pfs);
            Range range = hwpf.getRange();// 得到文档的读取范围
            TableIterator it = new TableIterator(range);
            String[] stu = new String[5];
            // 迭代文档中的表格
            while (it.hasNext()) {
                Table tb = (Table) it.next();
                // 迭代行,默认从0开始
                for (int i = 0; i < tb.numRows(); i++) {
                    TableRow tr = tb.getRow(i);
                    // 迭代列,默认从0开始
                    for (int j = 0; j < tr.numCells(); j++) {

                        TableCell td = tr.getCell(j);// 取得单元格
                        // 取得单元格的内容
                        String s = "";
                        for (int k = 0; k < td.numParagraphs(); k++) {
                            Paragraph para = td.getParagraph(k);// 获取第k个段落
                            s += para.text();
                            System.out.println(s);
                        }
                        s = s.replace("•", "");
                        stu[j] = s;

                        // end for
                    }// end for

                }
            }
        }catch (Exception e){

        }
    }
}

猜你喜欢

转载自blog.csdn.net/wangrongfei136/article/details/81777920