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. 【HDOJ】【1512】Monkey King

    数据结构/可并堆 啊……换换脑子就看了看数据结构……看了一下左偏树和斜堆,鉴于左偏树不像斜堆可能退化就写了个左偏树. 左偏树介绍:http://www.cnblogs.com/crazyac/arti ...

  2. Amazon Interview Question: Design an OO parking lot

    Design an OO parking lot. What classes and functions will it have. It should say, full, empty and al ...

  3. hdu 1863 畅通工程(最小生成树,基础)

    题目 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<string.h> #include <ma ...

  4. java基础知识回顾之---java StringBuffer类

    /*         * StringBuffer:就是字符串缓冲区,线程安全.         * 用于存储数据的容器.         * 特点:         * 1,长度的可变的.      ...

  5. IOS:利用dispatch_once创建单例

    在之前有一篇学习笔记中,记载了一篇如何在OC中实现单例的文章:<IOS学习笔记4—Objective C—创建单例>自苹果引入了Grand Central Dispatch (GCD)(M ...

  6. substr_replace()函数:将手机号中间4位隐藏为*号

    <?php $mobile = "15810320826"; echo substr_replace($mobile,'****',3 , 4); ?> substr_ ...

  7. TDD三个阶段

    TDD的三个阶段 红灯.绿灯.重构 :明确了实施TDD所要遵循的工作流 (需求--->测试-->代码[重构]) 红灯阶段:      为不存在的代码编写测试 绿灯阶段:      仅编写适 ...

  8. Oracle安装后,服务中没有监听器怎么处理?

    运行中输入netca 回车运行oracle net configuration assistant, 选择监听程序配置->下一步->接下来的步骤可以都选默认一直下一步到最后,即可.

  9. Protege汉字不能正常显示问题

    在Protege5.0中有下面的问题: 点击uses,汉字不能正常显示. 在qq群里面问到,可以通过设置label的方式,在对类,子类命名成英语的,点击annotations,在label中设置汉字名 ...

  10. 两个List合并,过滤重复记录

    import java.util.ArrayList; import java.util.HashSet; import java.util.Hashtable; import java.util.I ...