读书人

java 读取ppt并写下txt

发布时间: 2012-10-09 10:21:45 作者: rapoo

java 读取ppt并写入txt
//excel文件
private static String path="d:\\document\\1.ppt";

public static void main(String[] args) {
?? try {
??? // 得到源文件
??? SlideShow ss = new SlideShow(new HSLFSlideShow(path));
??? // 得到源文件中的幻灯片数量
??? Slide[] slides = ss.getSlides();
??? for (int a = 0; a < slides.length; a++) {
???? // 得到每张幻灯片中的字符串数量
???? Shape[] sps = slides[a].getShapes();
???? for (int i = 0; i < sps.length; i++) {
????? System.out.println(((TextBox) sps[i]).getText());
????? new PPT().insert(ppt, ((TextBox)sps[i]).getText(), true);
???? }
??? }
?? } catch (Exception e) {
??? e.getMessage();
?? }
}

/**
* 将文本写入相应的文本中
*/
public void insert(String path, String content, boolean append) {
?? BufferedWriter bw;
?? File file;
?? try {
??? boolean addstr = append;
??? file = new File(path);
??? // 创建文件输出流写入文件
??? FileWriter fw = new FileWriter(file, addstr);
??? bw = new BufferedWriter(fw);
??? // 将文本内容写入文件
??? fw.write(content);
??? fw.flush();
??? fw.close();
?? } catch (Exception e) {
??? e.getMessage();
?? }
}
}

1 楼 xin_ya_mei 2011-02-14 java.lang.ClassCastException: org.apache.poi.hslf.model.Picture cannot be cast to org.apache.poi.hslf.model.TextShape

System.out.println("+++++:"+((TextBox) sps[i]).getText()); // 出错处
new PowerPointConfig().insert( ppt, ( (TextShape)sps[i] ).getText(), true ) ;// 出错处

怎么解决

读书人网 >软件架构设计

热点推荐