java-poi3.17读取ppt与pptx文本和图片


package per.qy.dexter.fileoperate;

 

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.util.List;

import java.util.UUID;

 

import org.apache.poi.hslf.usermodel.HSLFSlideShow;

import org.apache.poi.hslf.usermodel.HSLFTable;

import org.apache.poi.hslf.usermodel.HSLFTableCell;

import org.apache.poi.hslf.usermodel.HSLFTextShape;

import org.apache.poi.sl.usermodel.PictureData;

import org.apache.poi.sl.usermodel.Shape;

import org.apache.poi.sl.usermodel.Slide;

import org.apache.poi.sl.usermodel.SlideShow;

import org.apache.poi.xslf.usermodel.XMLSlideShow;

import org.apache.poi.xslf.usermodel.XSLFTable;

import org.apache.poi.xslf.usermodel.XSLFTableCell;

import org.apache.poi.xslf.usermodel.XSLFTextShape;

import org.junit.Test;

 

public class PptTest {

 

    @SuppressWarnings({ "rawtypes", "unchecked" })

    @Test

    public void testPpt() {

        String path = "D:\\temp\\temp\\test.ppt";

        // String path = "D:\\temp\\temp\\test.pptx";

        File file = new File(path);

        InputStream is = null;

        SlideShow slideShow = null;

        try {

            is = new FileInputStream(file);

            if (path.endsWith(".ppt")) {

                slideShow = new HSLFSlideShow(is);

            } else if (path.endsWith(".pptx")) {

                slideShow = new XMLSlideShow(is);

            }

            if (slideShow != null) {

                // 文本内容

                StringBuilder content = new StringBuilder();

                // 一页一页读取

                for (Slide slide : (List<Slide>) slideShow.getSlides()) {

                    List shapes = slide.getShapes();

                    if (shapes != null) {

                        for (int i = 0; i < shapes.size(); i++) {

                            Shape shape = (Shape) shapes.get(i);

                            if (shape instanceof HSLFTextShape) {// 文本框

                                String text = ((HSLFTextShape) shape).getText();

                                content.append(text);

                            }

                            if (shape instanceof XSLFTextShape) {// 文本框

                                String text = ((XSLFTextShape) shape).getText();

                                content.append(text);

                            }

                            if (shape instanceof HSLFTable) {// 表格

                                int rowSize = ((HSLFTable) shape).getNumberOfRows();

                                int columnSize = ((HSLFTable) shape).getNumberOfColumns();

                                for (int rowNum = 0; rowNum < rowSize; rowNum++) {

                                    for (int columnNum = 0; columnNum < columnSize; columnNum++) {

                                        HSLFTableCell cell = ((HSLFTable) shape).getCell(rowNum, columnNum);

                                        if (cell != null) {

                                            String text = cell.getText();

                                            content.append(text);

                                        }

                                    }

                                }

                            }

                            if (shape instanceof XSLFTable) {// 表格

                                int rowSize = ((XSLFTable) shape).getNumberOfRows();

                                int columnSize = ((XSLFTable) shape).getNumberOfColumns();

                                for (int rowNum = 0; rowNum < rowSize; rowNum++) {

                                    for (int columnNum = 0; columnNum < columnSize; columnNum++) {

                                        XSLFTableCell cell = ((XSLFTable) shape).getCell(rowNum, columnNum);

                                        if (cell != null) {

                                            String text = cell.getText();

                                            content.append(text);

                                        }

                                    }

                                }

                            }

                        }

                    }

                    if (content.length() > 0) {

                        System.out.println(content);

                        content.delete(0, content.length());

                    }

                }

 

                // 图片内容

                List pictures = slideShow.getPictureData();

                for (int i = 0; i < pictures.size(); i++) {

                    PictureData picture = (PictureData) pictures.get(i);

                    byte[] data = picture.getData();

                    FileOutputStream out = new FileOutputStream("D:\\temp\\temp\\" + UUID.randomUUID() + ".jpg");

                    out.write(data);

                    out.close();

                }

            }

        } catch (FileNotFoundException e) {

        } catch (IOException e) {

        } finally {

            try {

                if (slideShow != null) {

                    slideShow.close();

                }

                if (is != null) {

                    is.close();

                }

            } catch (IOException e) {

            }

        }

    }

 

}
 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值