How to read content from ms word files using Jakarta POI

感情迁移 提交于 2019-12-20 05:24:49

问题


I've included jakarta-poi-1.5.1-final-20020615.jar file to read content from ms word.

I am unable to do this ...can anyone help me?


回答1:


Here is quick guide




回答2:


You need to move to a newer version of POI - the one you're on is about 9 years old! Grab the latest version of POI (it's just Apache POI now, hasn't been Apache Jakarta POI for a few years now), you'll want either 3.7 Final or 3.8 beta 2 as of writing.

Then, have a read through the HWPF docs and you should be good to go.




回答3:


Use this code with apache-poi

XWPFDocument doc = new XWPFDocument(new FileInputStream(fileName));
    List<XWPFTable> table = doc.getTables();
    for (XWPFTable xwpfTable : table) {
        List<XWPFTableRow> row = xwpfTable.getRows();
        for (XWPFTableRow xwpfTableRow : row) {
            List<XWPFTableCell> cell = xwpfTableRow.getTableCells();
            for (XWPFTableCell xwpfTableCell : cell) {
                if (xwpfTableCell != null) {
                    System.out.println(xwpfTableCell.getText());
                    String s = xwpfTableCell.getText();
                    for (XWPFParagraph p : xwpfTableCell.getParagraphs()) {
                        for (XWPFRun run : p.getRuns()) {
                            for (XWPFPicture pic : run.getEmbeddedPictures()) {
                                byte[] pictureData = pic.getPictureData().getData();
                                System.out.println("picture : " + pictureData);
                            }
                        }
                    }
                }
            }
        }
    }



回答4:


This method will print the internal runs of the entire document so you will be able to compare the values based on xml text.

for (XWPFParagraph p : doc.getParagraphs()) {
    for (XWPFRun r : p.getRuns()) {
       String text = r.getText(0);
       System.out.println(text);
     }
}


来源:https://stackoverflow.com/questions/4990139/how-to-read-content-from-ms-word-files-using-jakarta-poi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!