java - 如何使用apache poi从.docx文档中获取图片和表格?

标签 java apache apache-poi

亲爱的,我尝试将整个文档从 .docx 文件提取到 java 中的文本区域,但我只收到没有图像或表格的文本,所以有什么建议吗?提前致谢。

我的代码是:

try{
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
XWPFDocument doc = new XWPFDocument(new 
FileInputStream(chooser.getSelectedFile()));
XWPFWordExtractor extract = new XWPFWordExtractor(doc);
content.setText(extract.getText());
content.setFont(new Font("Serif", Font.ITALIC, 16));
content.setLineWrap(true);
content.setWrapStyleWord(true);
content.setBackground(Color.white);

} catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
} 

最佳答案

提取表格使用List<XWPFTable> table = doc.getTables()

下面的例子

public static void readWordDocument() { 
try { 
        String fileName = "C:\\sample.docx"; 

        if(!(fileName.endsWith(".doc") || fileName.endsWith(".docx"))) { 
                throw new FileFormatException(); 
        } else { 

        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()); 
                                                                            List<XWPFTable> itable = xwpfTableCell.getTables(); 
                                                                            if(itable.size()!=0) 
                                                                            { 
                                                                                    for (XWPFTable xwpfiTable : itable) { 
                                                                                            List<XWPFTableRow> irow = xwpfiTable.getRows(); 
                                                                                            for (XWPFTableRow xwpfiTableRow : irow) { 
                                                                                                    List<XWPFTableCell> icell = xwpfiTableRow.getTableCells(); 
                                                                                                    for (XWPFTableCell xwpfiTableCell : icell) { 
                                                                                                            if(xwpfiTableCell!=null) 
                                                                                                            {   
                                                                                                                    System.out.println(xwpfiTableCell.getText()); 
                                                                                                            } 
                                                                                                    } 
                                                                                            } 
                                                                                    } 
                                                                            } 
                                                                    } 
                                                            } 
                                                    } 
                } 
        } 
} catch(FileFormatException e) { 
        e.printStackTrace(); 
} catch (FileNotFoundException e) { 
        e.printStackTrace(); 
} catch (IOException e) { 
        e.printStackTrace(); 
} 

}

提取图像使用List<XWPFPictureData> piclist=docx.getAllPictures()

看下面的例子

    public static void extractImages(String src){
  try{

  //create file inputstream to read from a binary file
  FileInputStream fs=new FileInputStream(src);
  //create office word 2007+ document object to wrap the word file
  XWPFDocument docx=new XWPFDocument(fs);
  //get all images from the document and store them in the list piclist
  List<XWPFPictureData> piclist=docx.getAllPictures();
  //traverse through the list and write each image to a file
  Iterator<XWPFPictureData> iterator=piclist.iterator();
  int i=0;
  while(iterator.hasNext()){
   XWPFPictureData pic=iterator.next();
   byte[] bytepic=pic.getData();
   BufferedImage imag=ImageIO.read(new ByteArrayInputStream(bytepic));
          ImageIO.write(imag, "jpg", new File("D:/imagefromword"+i+".jpg"));
          i++;
  }

  }catch(Exception e){System.exit(-1);}

 }

关于java - 如何使用apache poi从.docx文档中获取图片和表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44280677/

相关文章:

java - 我试图使用 CountDownTimer 每秒将值放入数组中,但它不起作用

php - 如何在 Windows 计算机上使用 PHP 旋转 pdf 内容?

java - apache poi - 多次循环同一数组

java - 使用 poi api 从电子表格中读取时间值

java - 准备好的查询的奇怪错误不是 SELECT_LONG 类型

java - 使用 libgdx 在 Android Studio 中进行热代码交换

java - 改变深度优先搜索的方向

php - 如何以编程方式从 Linux 中的 Apache 重新启动系统服务(不是 Apache)?

Apache:如何在错误页面上使用替代品?

java - Apache POI - 读取由 TEXT() 公式格式化的单元格