tang5324110 发表于 2013-2-3 11:21:19

java 读取ppt并写入txt

<div class="cnt">package document;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.poi.hslf.HSLFSlideShow;
import org.apache.poi.hslf.model.Shape;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.model.TextBox;
import org.apache.poi.hslf.usermodel.SlideShow;
/**
* 将PPT中的内容复制到txt中
* @author DanielCooger
* <a href="mailto:tangjunfeng52099@gmail.com">daniel</a>
*/
public class PPT {
private static String date = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
//新建txt文件
private static String ppt = "d:\\doc\\PPT" + date + ".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.getShapes();
     for (int i = 0; i < sps.length; i++) {
      System.out.println(((TextBox) sps).getText());
      new PPT().insert(ppt, ((TextBox)sps).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]
查看完整版本: java 读取ppt并写入txt