POI 读word 图片 和 读word表格

转帖:http://blog.csdn.net/InviteSun/archive/2009/10/16/4682765.aspx

 

POI 读word表格

view plaincopy to clipboardprint?
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());  
        } 
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里读出表格并不是目的,如何定位到表格的位置,才是重要的。没有解决...


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/InviteSun/archive/2009/10/16/4682765.aspx

 

POI 读word 图片

view plaincopy to clipboardprint?
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();  
        }  
    } 
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源码里就有...

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


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/InviteSun/archive/2009/10/16/4682904.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值