How to read content from ms word files using Jakarta POI

后端 未结 4 1751
忘掉有多难
忘掉有多难 2021-01-25 15:58

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?

相关标签:
4条回答
  • 2021-01-25 16:10

    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);
                                }
                            }
                        }
                    }
                }
            }
        }
    
    0 讨论(0)
  • 2021-01-25 16:21

    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.

    0 讨论(0)
  • 2021-01-25 16:24

    Here is quick guide

    0 讨论(0)
  • 2021-01-25 16:28

    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);
         }
    }
    
    0 讨论(0)
提交回复
热议问题