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 {
@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 : 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) {
}
}
}
}
java-poi4.0.1读取ppt文本和图片
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...