POI读取Word与Excel
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的更多相关文章
- java操作office和pdf文件java读取word,excel和pdf文档内容
在平常应用程序中,对office和pdf文档进行读取数据是比较常见的功能,尤其在很多web应用程序中.所以今天我们就简单来看一下Java对word.excel.pdf文件的读取.本篇博客只是讲解简单应 ...
- java使用poi读取word(简单,简约,直观)
java使用poi读取word(简单,简约,直观) 说明 其实poi的官网上面都是有接口和样例的,只是都是英文 例如网址:http://poi.apache.org/spreadsheet/quick ...
- POI 读取word (word 2003 和 word 2007) (转)
最近在给客户做系统的时候,用户提出需求,要能够导入 word 文件,现在 microsoft word 有好几个版本 97.2003.2007的,这三个版本存储数据的格式上都有相当大的差别,而现在 9 ...
- Java使用POI读取和写入Excel指南
Java使用POI读取和写入Excel指南 做项目时经常有通过程序读取Excel数据,或是创建新的Excel并写入数据的需求: 网上很多经验教程里使用的POI版本都比较老了,一些API在新版里已经废弃 ...
- POI 读取word (word 2003 和 word 2007)(转,好用)
POI 读取word (word 2003 和 word 2007)(转,好用) 转做的操作: 将作者文中失效的链接的正确链接放在失效链接的下面. 最近在给客户做系统的时候,用户提出需求,要能够导入 ...
- java实现在线预览--poi实现word、excel、ppt转html
java实现在线预览 - -之poi实现word.excel.ppt转html 简介 java实现在线预览功能是一个大家在工作中也许会遇到的需求,如果公司有钱,直接使用付费的第三方软件或者云在线预览服 ...
- java实现在线预览 - -之poi实现word、excel、ppt转html
简介 java实现在线预览功能是一个大家在工作中也许会遇到的需求,如果公司有钱,直接使用付费的第三方软件或者云在线预览服务就可以了,例如永中office.office web 365(http://w ...
- php 如何写入、读取word,excel文档
如何在php写入.读取word文档 <? //如何在php写入.读取word文档 // 建立一个指向新COM组件的索引 $word = new COM("word.applicatio ...
- Java中使用POI读取大的Excel文件或者输入流时发生out of memory异常参考解决方案
注意:此参考解决方案只是针对xlsx格式的excel文件! 背景 前一段时间遇到一种情况,服务器经常宕机,而且没有规律性,查看GC日志发生了out of memory,是堆溢出导致的,分析了一下堆的d ...
随机推荐
- 30个实用的Linux find命令示例
除了在一个目录结构下查找文件这种基本的操作,你还可以用find命令实现一些实用的操作,使你的命令行之旅更加简易. 本文将介绍15种无论是于新手还是老鸟都非常有用的Linux find命令. 首先,在你 ...
- 【BZOJ】【3158】千钧一发
网络流/最小割 这题跟BZOJ 3275限制条件是一样的= =所以可以用相同的方法去做……只要把边的容量从a[i]改成b[i]就行了- (果然不加当前弧优化要略快一点) /************** ...
- nenu contest3
http://vjudge.net/contest/view.action?cid=55702#overview 12656 - Almost Palindrome http://uva.online ...
- uva 11235
数据结构 RMQ算法 左右左右 写得有点晕了 ..... /****************************************************************** ...
- mac下SVN上传.a静态库文件
在mac下很多svn管理工具默认都不能上传.a文件,但是用命令行可以解决此问题. 打开终端,cd 进入到需要上传的.a文件所在的文件夹. 确保 ls能看到.a文件 然后使用命令,如:svn add l ...
- 【C++基础】指针好难啊,一点点啃——基本概念
指针保存的是另一个对象的地址(概念真的很重要!!) ; int *ptr = &a;//*定义一个指向int类型的指针ptr, &a取变量a的地址 引用是对象的别名,多用于函数形参,引 ...
- hdu 4417 Super Mario 离线线段树
思路:将点按值从小到大排序,询问按h从小到大排序. 在建立线段树,按h的大小更新树并得到该次查询的结果! 代码如下: #include<iostream> #include<stdi ...
- nmap使用详解
nmap是一个网络探测和安全扫描程序, 系统管理者和个人可以使用这个软件扫描大型的网络,获取那台主机正在运行以及提供什么服务等信息.nmap支持很多扫描技术,例如:UDP.TCP connect(). ...
- intellij idea 注释行如何自动缩进?
我们指代代码注释的快捷键是 Ctrl+/ 但是加注释后"//"自动放在行首,没缩进,就像这样
- Excel多条件筛选求和
单位A 代码B 面积(㎡)C A组 011 124 A组 123 15 A组 011 356 A组 123 44 B组 123 31 B组 011 2 B组 123 2 按照单位和代码求面积的和,可以 ...