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. matlab中读取txt数据文件(txt文本文档)

    matlab中读取txt数据文件(txt文本文档) 根据txt文档不同种类介绍不同的读取数据方法 一.纯数据文件(没有字母和中文,纯数字) 对于这种txt文档,从matalb中读取就简单多了 例如te ...

  2. Linux操作系统下软件的安装方法大全

    一.rpm包安装方式步骤: 1.找到相应的软件包,比如soft.version.rpm,下载到本机某个目录: 2.打开一个终端,su -成root用户: 3.cd soft.version.rpm所在 ...

  3. 如何有效地记录 Java SQL 日志?

    在常规项目的开发中可能最容易出问题的地方就在于对数据库的处理了,在大部分的环境下,我们对数据库的操作都是使用流行的框架,比如 Hibernate . MyBatis 等.由于各种原因,我们有时会想知道 ...

  4. 【面试题030】最小的k个数

    [面试题030]最小的k个数 题目:     输入n个整数,找出其中最小的k个数.     例如输入4.5.1.6.2.7.3.8这8个字,则其中最小的4个数字是1.2.3.4.     思路一:   ...

  5. hdu 1757 A Simple Math Problem (矩阵快速幂,简单)

    题目 也是和LightOJ 1096 和LightOJ 1065 差不多的简单题目. #include<stdio.h> #include<string.h> #include ...

  6. 【QT】OpenCV配置

    很郁闷的表示我的opencv放在 D:\\program files  里面路径有个空格,导致我不得不把整个opencv又拷贝到了一个没有空格的路径下面命名为opencvForQt 网上有各种用CMa ...

  7. android音乐播放器开发教程

    android音乐播放器开发教程 Android扫描sd卡和系统文件 Android 关于录音文件的编解码 实现米聊 微信一类的录音上传的功能 android操作sdcard中的多媒体文件——音乐列表 ...

  8. ubantu安装jdk来配置hadoop

    1.将jdk-7u5-linux-x64.tar.gz拷贝到/usr/lib/jdk/目录下面,这里如果没有jdk文件夹,则创建该文件夹,命令: sudo mkdir jdk //创建文件夹jdk s ...

  9. C 语言学习guideline

    Kernighan和Ritchie的<The C Programming Language>(中译名<C程序设计语言>)堪称经典中的经典,不过旧版的很多内容都已过时,和现在的标 ...

  10. 31. Next Permutation

    题目: Implement next permutation, which rearranges numbers into the lexicographically next greater per ...