import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.File;
import java.io.OutputStreamWriter;
import java.util.HashSet; import org.apache.poi.hslf.HSLFSlideShow;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.model.TextRun;
import org.apache.poi.hslf.usermodel.RichTextRun;
import org.apache.poi.hslf.usermodel.SlideShow;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.hwpf.usermodel.CharacterRun;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.Section;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFShape;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.apache.poi.xslf.usermodel.XSLFTable;
import org.apache.poi.xslf.usermodel.XSLFTableCell;
import org.apache.poi.xslf.usermodel.XSLFTableRow;
import org.apache.poi.xslf.usermodel.XSLFTextParagraph;
import org.apache.poi.xslf.usermodel.XSLFTextRun;
import org.apache.poi.xslf.usermodel.XSLFTextShape;
public class read { private static InputStream is =null; public static void readFiles(String path){
File f = new File(path);
File[] files = f.listFiles();
for(File everyfile : files){
StringBuilder sb = new StringBuilder();
if(everyfile.isDirectory()) continue;
String Filename = everyfile.getName();
if(Filename.startsWith("~")) continue;
if(!Filename.endsWith("doc")) continue;
System.out.println(Filename);
sb.append("###@@@").append(Filename.substring(,Filename.lastIndexOf("."))).append("\n");
sb.append("----------------------").append("\n");
try {
//输入文件流
is = new FileInputStream(everyfile);
if(Filename.toLowerCase().endsWith("ppt")||Filename.toLowerCase().endsWith("pptm")){
try {
XMLSlideShow pptx = new XMLSlideShow(is);
is.close();
for(int x= ; x< pptx.getSlides().length ; x++){
XSLFSlide slide = pptx.getSlides()[x];
if (slide.getShapes().length == ) continue;
String title = getTitle(slide);
if(title != null) sb.append(title).append("\t").append("title##@@").append("\n");
for(XSLFShape shape : slide){
if(shape instanceof XSLFTextShape){
XSLFTextShape content = (XSLFTextShape)shape;
for( XSLFTextParagraph ttp: content.getTextParagraphs()){ if(ttp.getText().equals(title)) continue;
//用一个set统计到底有多少字体大小,如果只有一种字体大小,则直接添加paragraph
HashSet<Float> sizeset = new HashSet<Float>();
for(XSLFTextRun tr : ttp.getTextRuns()){
if (tr.getText().trim().equals("")) continue;
if(tr.getText().trim().equals(title)) continue;
float size = (float) tr.getFontSize();
sizeset.add(size);
}
if(sizeset.size()!=){
for(XSLFTextRun tr : ttp.getTextRuns()){
if (tr.getText().trim().equals("")) continue;
if(tr.getText().trim().equals(title)) continue;
String text = tr.getText();
float size = (float) tr.getFontSize();
sb.append(text.trim()).append("\t").append(size).append("##@@").append("\n");
}
}else{
sb.append(ttp.getText().trim().replaceAll("[\\n\\r]", " ")).append("\t").append((float)sizeset.toArray()[]).append("##@@").append("\n"); }
}
}else if(shape instanceof XSLFTable){
XSLFTable txShape = (XSLFTable)shape;
for(XSLFTableRow row : txShape.getRows()){
for(XSLFTableCell cell: row.getCells()){
XSLFTextShape content = (XSLFTextShape)cell;
for( XSLFTextParagraph ttp: content.getTextParagraphs()){
if(ttp.getText().equals(title)) continue;
//用一个set统计到底有多少字体大小,如果只有一种字体大小,则直接添加paragraph HashSet<Float> sizeset = new HashSet<Float>();
for(XSLFTextRun tr : ttp.getTextRuns()){
if (tr.getText().trim().equals("")) continue;
if(tr.getText().trim().equals(title)) continue;
float size = (float) tr.getFontSize();
sizeset.add(size);
}
if(sizeset.size()!=){
for(XSLFTextRun tr : ttp.getTextRuns()){
if (tr.getText().trim().equals("")) continue;
if(tr.getText().trim().equals(title)) continue;
String text = tr.getText();
float size = (float) tr.getFontSize();
sb.append(text.trim()).append("\t").append(size).append("##@@").append("\n");
}
}else{
sb.append(ttp.getText().trim().replaceAll("[\\n\\r]", " ")).append("\t").append((float)sizeset.toArray()[]).append("##@@").append("\n"); }
}
}
}
}
}
if(x!=pptx.getSlides().length-) sb.append("----------------------").append("\n");
}
} catch (IOException e) {
e.printStackTrace();
} }else if(Filename.endsWith("ppt")){ try {
SlideShow ss = new SlideShow(new HSLFSlideShow(is)); is.close();
for(int x = ; x < ss.getSlides().length ; x ++){
Slide slide = ss.getSlides()[x];
if (slide.getShapes().length ==) continue;
String title = getTitle(slide);
if(title != null) sb.append(title).append("\t").append("title##@@").append("\n"); for(TextRun tr : slide.getTextRuns()){ HashSet<Float> sizeset = new HashSet<Float>(); for(RichTextRun rtr : tr.getRichTextRuns()){
if (rtr.getText().trim().equals("")|| rtr.getText() ==null) continue;
if(rtr.getText().trim().equals(title)) continue;
sizeset.add((float)rtr.getFontSize());
}
if(sizeset.size()!=){
for(RichTextRun rtr : tr.getRichTextRuns()){
if (rtr.getText().trim().equals("") || rtr.getText() ==null) continue;
if(rtr.getText().trim().equals(title)) continue;
String text = rtr.getText();
float size = (float) rtr.getFontSize();
sb.append(text.trim()).append("\t").append(size).append("##@@").append("\n");
}
}else {
for(RichTextRun rtr : tr.getRichTextRuns()){
if (rtr.getText().trim().equals("")|| rtr.getText() ==null) continue;
if(rtr.getText().trim().equals(title)) continue;
sb.append(rtr.getText().trim()).append(" ");
}
sb.append("\t").append((float)sizeset.toArray()[]).append("##@@").append("\n");
}
}
if(x!=ss.getSlides().length-) sb.append("----------------------").append("\n");
}
} catch (IOException e) {
e.printStackTrace();
}
}else if(Filename.endsWith("doc")){
try {
HWPFDocument hwpf = new HWPFDocument(is);
Range range = hwpf.getRange();
for (int x = ; x < range.numSections(); x++) {
Section s = range.getSection(x);
for (int y = ; y < s.numParagraphs(); y++) {
Paragraph p = s.getParagraph(y);
for (int z = ; z < p.numCharacterRuns(); z++) {
CharacterRun run = p.getCharacterRun(z);
//字符串文本
String text = run.text().trim();
if(text ==null ||text == " "|| text=="") continue;
sb.append(text.trim()).append("\t").append(run.getFontSize()).append("##@@").append("\n");
}
}
if (x != range.numSections()-) sb.append("----------------------").append("\n");
}
} catch (IOException e) {
e.printStackTrace();
} }
} catch (FileNotFoundException e) {
e.printStackTrace();
}
write(sb.toString());
}
} public static String getTitle(XSLFSlide slide){
String title = null;
if (slide.getTitle() != null &&
!slide.getTitle().trim().equals("")){
title= slide.getTitle().trim();
} return title; } public static String getTitle(Slide slide){
String title = null;
if (slide.getTitle() != null &&
!slide.getTitle().trim().equals("")){
title= slide.getTitle().trim();
}
return title;
}
static FileOutputStream fos =null;
static OutputStreamWriter osw =null;
static BufferedWriter bw =null;
public static void write(String content){
File f = new File("ressss.csv");
try {
fos = new FileOutputStream(f,true);
osw = new OutputStreamWriter(fos,"utf-8");
bw = new BufferedWriter(osw);
bw.write(content);
bw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{ if(bw !=null){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
bw =null;
}
if(osw !=null){
try {
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
osw =null;
}
if(fos !=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
fos =null;
}
}
} public static void main(String[] args) throws Exception {
readFiles("C:\\Users\\ooon\\Desktop\\DKM_data\\DKM_data");
}
}

POI读取Word与Excel的更多相关文章

  1. java操作office和pdf文件java读取word,excel和pdf文档内容

    在平常应用程序中,对office和pdf文档进行读取数据是比较常见的功能,尤其在很多web应用程序中.所以今天我们就简单来看一下Java对word.excel.pdf文件的读取.本篇博客只是讲解简单应 ...

  2. java使用poi读取word(简单,简约,直观)

    java使用poi读取word(简单,简约,直观) 说明 其实poi的官网上面都是有接口和样例的,只是都是英文 例如网址:http://poi.apache.org/spreadsheet/quick ...

  3. POI 读取word (word 2003 和 word 2007) (转)

    最近在给客户做系统的时候,用户提出需求,要能够导入 word 文件,现在 microsoft word 有好几个版本 97.2003.2007的,这三个版本存储数据的格式上都有相当大的差别,而现在 9 ...

  4. Java使用POI读取和写入Excel指南

    Java使用POI读取和写入Excel指南 做项目时经常有通过程序读取Excel数据,或是创建新的Excel并写入数据的需求: 网上很多经验教程里使用的POI版本都比较老了,一些API在新版里已经废弃 ...

  5. POI 读取word (word 2003 和 word 2007)(转,好用)

    POI 读取word (word 2003 和 word 2007)(转,好用) 转做的操作: 将作者文中失效的链接的正确链接放在失效链接的下面. 最近在给客户做系统的时候,用户提出需求,要能够导入 ...

  6. java实现在线预览--poi实现word、excel、ppt转html

    java实现在线预览 - -之poi实现word.excel.ppt转html 简介 java实现在线预览功能是一个大家在工作中也许会遇到的需求,如果公司有钱,直接使用付费的第三方软件或者云在线预览服 ...

  7. java实现在线预览 - -之poi实现word、excel、ppt转html

    简介 java实现在线预览功能是一个大家在工作中也许会遇到的需求,如果公司有钱,直接使用付费的第三方软件或者云在线预览服务就可以了,例如永中office.office web 365(http://w ...

  8. php 如何写入、读取word,excel文档

    如何在php写入.读取word文档 <? //如何在php写入.读取word文档 // 建立一个指向新COM组件的索引 $word = new COM("word.applicatio ...

  9. Java中使用POI读取大的Excel文件或者输入流时发生out of memory异常参考解决方案

    注意:此参考解决方案只是针对xlsx格式的excel文件! 背景 前一段时间遇到一种情况,服务器经常宕机,而且没有规律性,查看GC日志发生了out of memory,是堆溢出导致的,分析了一下堆的d ...

随机推荐

  1. windows下编译Libevent

    下载最新的libevent,目前是 libevent-2.0.21-stable.tar.gz 修改“D:\libevent-2.0.21-stable\event_iocp.c”.“D:\libev ...

  2. log4j记录exception异常有价值信息及log4j级别

    try{ }catch(Exception e){ e.printStackTrace(); log.error("配件导出excel错误:", e.fillInStackTrac ...

  3. Curl的毫秒超时的一个”Bug”

    Curl的毫秒超时的一个”Bug” -- PHP老杨 最近我们的服务在升级php使用的libcurl, 期望新版本的libcurl支持毫秒级的超时, 从而可以更加精细的控制后端的接口超时, 从而提高整 ...

  4. DIY Ruby CPU 分析——Part I

    [编者按]原文作者 Emil Soman,Rubyist,除此之外竟然同时也是艺术家,吉他手,Garden City RubyConf 组织者.本文是DIY Ruby CPU Profiling 的第 ...

  5. 在实体注解OneToMany时,要加上mappedby,避免产生中间表。

    在实体注解OneToMany时,要加上mappedby,避免产生中间表.

  6. 由CHAR(2)引发的BUG

    我们在设计数据库标志位字段时,为考虑其扩展性,一般会设置为CHAR(2),例如 FLAG CHAR(2),这样我们就需要注意了,如果你给字段 FLAG赋值为‘0’,它在数据库中的真实情况是‘0+空格’ ...

  7. Ubuntu 取消sudo密码

    需求:在Ubuntu下装了FQ代理goagent之后,为了goagent能够开机启动.因为goagent需要sudo权限,所以要去掉sudo密码. 要修改的文件位于/etc/sudoers,先备份: ...

  8. 日志工具logback的简介与配置

    Logback是由log4j创始人设计的又一个开源日志组件.logback当前分成三个模块:logback-core,logback- classic和logback-access.logback-c ...

  9. lintcode 中等题:permutations 全排列

    题目 全排列 给定一个数字列表,返回其所有可能的排列. 您在真实的面试中是否遇到过这个题? Yes 样例 给出一个列表[1,2,3],其全排列为: [ [1,2,3], [1,3,2], [2,1,3 ...

  10. unity UGUI动态字体显示模糊

    设置Unity中ttf文件的Character为Unicode,点击apply