日期:2014-05-20 浏览次数:20876 次
package myservlet; import java.io.*; import org.apache.poi.hslf.HSLFSlideShow; import org.apache.poi.hslf.model.TextRun; import org.apache.poi.hslf.model.Slide; import org.apache.poi.hslf.usermodel.SlideShow; import org.apache.poi.hwpf.extractor.WordExtractor; public class Show implements Read{ public String show(String sd){ String read=new String(); String path=this.getClass().getResource("/").toString(); int index=path.indexOf("webapps"); path=path.substring(0,index+8); path=path.substring(6); if(sd.endsWith(".doc")){ StringBuffer content=new StringBuffer(); try{ FileInputStream in = new FileInputStream (path+"sourse/"+sd); WordExtractor extractor = new WordExtractor(in); final String str = extractor.getText(); int nd=str.length(); if(nd!=0){ for(int i=0;i<5&&(i+1)*30<nd;i++){ content.append(str.substring(i*30,(i+1)*30)+"<br>"); } }else { System.out.println("Read error!"); } }catch(final Exception e){ e.printStackTrace(); } read=content.toString(); } else if(sd.endsWith(".ppt")){ StringBuffer content = new StringBuffer(""); try{ HSLFSlideShow h=new HSLFSlideShow(path+"sourse/"+sd); SlideShow ss = new SlideShow(h); Slide[] slides = ss.getSlides();//获得每一张幻灯片 for(int i=0;i <slides.length;i++){ TextRun[] t = slides[i].getTextRuns();//为了取得幻灯片的文字内容,建立TextRun for(int j=0;j <t.length;j++){ content.append(t[j].getText());//这里会将文字内容加到content中去 } content.append(slides[i].getTitle()); } }catch(Exception ex){ ; ex.printStackTrace(); System.out.println(ex.toString()); } read=content.toString(); } return read; } }
------解决方案--------------------
学习...
------解决方案--------------------
顶下6楼,以前用poi写过excel文档,但是代码在公司不能贴出来,可惜了。
------解决方案--------------------