POI 读word 图片 和 读word表格

 

POI 读word表格

 

FileInputStream in = new FileInputStream("D://操作手册.doc");   
        POIFSFileSystem pfs = new POIFSFileSystem(in);   
        HWPFDocument hwpf = new HWPFDocument(pfs);   
        Range range = hwpf.getRange();   
        TableIterator it = new TableIterator(range);   
        int biaoShuNum = 1;   
        while (it.hasNext()) {   
            Table tb = (Table) it.next();   
            boolean tableBegin = true;   
            StringBuffer buffer = new StringBuffer();   
            buffer.insert(0, "表" + biaoShuNum++  + ":<br>");   
            for (int i = 0; i < tb.numRows(); i++) {   
                TableRow tr = tb.getRow(i);   
                boolean rowBegin = true;   
                for (int j = 0; j < tr.numCells(); j++) {   
                    TableCell td = tr.getCell(j);   
                    if(tableBegin) {   
                        buffer.append("<table border=1>");   
                        tableBegin = false;   
                    }   
                    if(rowBegin) {   
                        buffer.append("<tr>");   
                        rowBegin = false;   
                    }   
                    buffer.append("<td>");   
                    for(int k=0;k<td.numParagraphs();k++){   
                        Paragraph p =td.getParagraph(k);   
                        String s = p.text();   
                        buffer.append(s.replaceAll("", "")).append("<br/>");   
                    }   
                    buffer.append("</td>");   
                }   
                buffer.append("</tr>");   
            }   
            buffer.append("</table>");   
            System.out.println(buffer.toString());   
        }  

  }
 

从word里读出表格并不是目的,如何定位到表格的位置,才是重要的。没有解决...

 

POI 读word 图片

 

public static void main(String args[]) {   
        try {   
            String path = "D:/测试word.DOC";   
            FileInputStream in = new FileInputStream(new File(path));   
            HWPFDocument doc = new HWPFDocument(in);   
            PicturesTable pic = doc.getPicturesTable();   
            List pictureList = pic.getAllPictures();   
            System.out.println(pictureList.size());   
            BufferedOutputStream output = null;   
            for(int i=0;i<pictureList.size();i++) {   
                Picture p = (Picture)pictureList.get(i);   
                output = new BufferedOutputStream(new FileOutputStream("D:/img/" + (i+1) + ".jpg"));   
                output.write(p.getContent());   
                output.flush();   
                output.close();   
            }   
        } catch (Exception ex) {   
            ex.printStackTrace();   
        }   
    }  
 
 

这个例子poi源码里就有...

图片的位置变通一下,是可以定位的。但是不敢保证一定准确...

你可能感兴趣的:(word)