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. select()2

    只要接触过c/c++网路编程人都可能会知道select io 模式,网络书籍都说 fd_set {]} 有所限制,因为数组的长度只有64,那么超过64你就不能放,要么你就是用多线程分别实用select ...

  2. ASP.NET WebAPI2 发布之后404 Note Found

    方法一:首先确保服务器安装.Net FrameWork 4.0 并且注册IIS 如果先安装.net framework4.0,再安装IIS,则会出现4.0的ISAPI没有注册的情况. 运行:C:\Wi ...

  3. c#中HttpWebRequest使用Proxy实现指定IP的域名请求

    原文:http://www.cnblogs.com/greenerycn/archive/2010/04/11/httpwebreques_host_modify_By_set_proxy.html ...

  4. C/C++框架和库

    http://blog.csdn.net/xiaoxiaoyeyaya/article/details/42541419 值得学习的C语言开源项目 - 1. Webbench Webbench是一个在 ...

  5. 深入理解Tornado——一个异步web服务器

    本人的第一次翻译,转载请注明出处:http://www.cnblogs.com/yiwenshengmei/archive/2011/06/08/understanding_tornado.html原 ...

  6. Tries

    trie,又称前缀树或字典樹,是一种有序树,用于保存关联数组,其中的键通常是字符串.与二叉查找树不同,键不是直接保存在节点中,而是由节点在树中的位置决定.一个节点的所有子孙都有相同的前缀,也就是这个节 ...

  7. poj 1797 Heavy Transportation(最短路变种2,连通图的最小边)

    题目 改动见下,请自行画图理解 具体细节也请看下面的代码: 这个花了300多ms #define _CRT_SECURE_NO_WARNINGS #include<string.h> #i ...

  8. Play Framework 2.2.6 安装

    网络上很多安装方法都是互相复制黏贴的, 都没有人考虑到启动application 还有依赖很多jar 包,而其中typesafe 官网提供的只是一个mini的 启动器来安装,很慢,所以以下下载完整包. ...

  9. 关于JS中变量的作用域-实例

    先看问题,如下,自己运行一下吧! if (!('_qyzA' in window)) { var _qyzA = 1; } alert(_qyzA);//undefined 分析:首先,所有的全局变量 ...

  10. untiy 插件工具: 游戏中 策划数据Excel 导出到项目中

    https://github.com/zhutaorun/Excel2Unity,这个项目是直接下载就可以用的, 其中原理和相关的解释 http://blog.csdn.net/neil3d/arti ...