java 解析office文件 大全
原文地址:http://ansjsun.iteye.com/blog/791142
读取OFFICE文件纯文本
package org.css.resource.businesssoft.searchengine.quwenjiansuo; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream; import org.apache.poi.POITextExtractor;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.POIXMLTextExtractor;
import org.apache.poi.extractor.ExtractorFactory;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.xmlbeans.XmlException;
/**
*
* @author lizh
*
*/
public class CovertFile { /**
* 从word 2003文档中提取纯文本
* @param is
* @return
* @throws IOException
*/
public static String extractTextFromDOC(InputStream is) throws IOException {
WordExtractor ex = new WordExtractor(is); // is是WORD文件的InputStream
return ex.getText();
} /**
* 从word 2007文档中提取纯文本
* @param fileName
* @return
*/
public static String extractTextFromDOC2007(String fileName) {
try {
OPCPackage opcPackage = POIXMLDocument.openPackage(fileName);
POIXMLTextExtractor ex = new XWPFWordExtractor(opcPackage);
return ex.getText();
} catch (Exception e) {
return "";
}
} /**
* 从excel 2003文档中提取纯文本
* @param is
* @return
* @throws IOException
*/
private static String extractTextFromXLS(InputStream is) throws IOException {
StringBuffer content = new StringBuffer();
HSSFWorkbook workbook = new HSSFWorkbook(is); // 创建对Excel工作簿文件的引用 for (int numSheets = 0; numSheets < workbook.getNumberOfSheets(); numSheets++) {
if (null != workbook.getSheetAt(numSheets)) {
HSSFSheet aSheet = workbook.getSheetAt(numSheets); // 获得一个sheet for (int rowNumOfSheet = 0; rowNumOfSheet <= aSheet
.getLastRowNum(); rowNumOfSheet++) {
if (null != aSheet.getRow(rowNumOfSheet)) {
HSSFRow aRow = aSheet.getRow(rowNumOfSheet); // 获得一行 for (short cellNumOfRow = 0; cellNumOfRow <= aRow
.getLastCellNum(); cellNumOfRow++) {
if (null != aRow.getCell(cellNumOfRow)) {
HSSFCell aCell = aRow.getCell(cellNumOfRow); // 获得列值 if (aCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
content.append(aCell.getNumericCellValue());
} else if (aCell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {
content.append(aCell.getBooleanCellValue());
} else {
content.append(aCell.getStringCellValue());
}
}
}
}
}
}
}
return content.toString();
} /**
* 从excel 2007文档中提取纯文本
* @param fileName
* @return
* @throws Exception
*/
private static String extractTextFromXLS2007(String fileName)
throws Exception {
StringBuffer content = new StringBuffer(); // 构造 XSSFWorkbook 对象,strPath 传入文件路径
XSSFWorkbook xwb = new XSSFWorkbook(fileName); // 循环工作表Sheet
for (int numSheet = 0; numSheet < xwb.getNumberOfSheets(); numSheet++) {
XSSFSheet xSheet = xwb.getSheetAt(numSheet);
if (xSheet == null) {
continue;
} // 循环行Row
for (int rowNum = 0; rowNum <= xSheet.getLastRowNum(); rowNum++) {
XSSFRow xRow = xSheet.getRow(rowNum);
if (xRow == null) {
continue;
} // 循环列Cell
for (int cellNum = 0; cellNum <= xRow.getLastCellNum(); cellNum++) {
XSSFCell xCell = xRow.getCell(cellNum);
if (xCell == null) {
continue;
} if (xCell.getCellType() == XSSFCell.CELL_TYPE_BOOLEAN) {
content.append(xCell.getBooleanCellValue());
} else if (xCell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
content.append(xCell.getNumericCellValue());
} else {
content.append(xCell.getStringCellValue());
}
}
}
} return content.toString();
} /**
* 从excel 2007文档中提取纯文本
* @param fileName
* @return
*/
public static String getXLS2007(String fileName){
String doc = "";
try{
doc = extractTextFromXLS2007(fileName);
return doc;
}catch(Exception e){
return "";
}
} /**
* 从ppt 2003、2007文档中提取纯文本
* @param fileName
* @return
*/
public static String getPPTX(String fileName){
String doc = "";
try{
File inputFile = new File(fileName);
POITextExtractor extractor = ExtractorFactory.createExtractor(inputFile);
doc = extractor.getText();
return doc;
}catch(Exception e){
return "";
}
} public static void main(String[] args) {
try {
// String wordFile = "D:/松山血战.docx";
// String wordText2007 = CovertFile.extractTextFromDOC2007(wordFile);
// System.out.println("wordText2007=======" + wordText2007);
//
// InputStream is = new FileInputStream("D:/XXX研发中心技术岗位职位需求.xls");
// String excelText = CovertFile.extractTextFromXLS(is);
// System.out.println("text2003==========" + excelText); // String excelFile = "D:/zh.xlsx";
// String excelText2007 = CovertFile.extractTextFromXLS2007(excelFile);
// System.out.println("excelText2007==========" + excelText2007); String pptFile = "D:/zz3.ppt";
String pptx = CovertFile.getPPTX(pptFile);
System.out.println("pptx==========" + pptx); } catch (Exception e) {
e.printStackTrace();
}
} }
最后突然发现其实只用两行代码就能搞定
office 2003 - office 2007
POITextExtractor extractor = ExtractorFactory.createExtractor(f);
return extractor.getText();
于是我泪流满面....白忙乎了..顺路奉上解析pdf的吧
package com.lingjoin.extractors; import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.StringReader;
import java.util.Date; import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentInformation;
import org.apache.pdfbox.util.PDFTextStripper;
import com.lingjoin.paser.LingJoinFile; /**
* PDF解析器
*
* @author Ansj
*
*/
public class PDFExtractor extends AbstractExtractor { private String getContent(LingJoinFile f) {
// TODO Auto-generated method stub
PDDocument doc = null ;
try {
doc = PDDocument.load(f);
PDFTextStripper stripper = new PDFTextStripper();
/**
* 设置文件的信息
*/
this.setLingJoinFileInfo(f, doc
.getDocumentInformation());
return stripper.getText(doc);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (doc != null) {
try {
doc.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return "";
} private BufferedReader getContentReader(LingJoinFile f) {
return new BufferedReader(new StringReader(this.getContent(f)));
} /**
*
* 项目名称:FilePaser
* 类描述: 设置文件的信息
* 创建人:ANSJ
* 创建时间:2010-4-14 下午04:27:57
* 修改备注:
* @version
*/
private void setLingJoinFileInfo(LingJoinFile f, PDDocumentInformation info) {
if (info.getAuthor() != null) {
f.setlAuthor(info.getAuthor());
}
// try {
// if (info.getModificationDate() != null) {
// Date date = info.getModificationDate().getTime();
// f.setlModificationDate(date.getTime());
// }
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//设置标题
// if (info.getTitle() != null) {
// f.setlTitle(info.getTitle());
// }
} public void paserFileToReader(LingJoinFile f) throws Exception {
f.setlContentReader(this.getContentReader(f)) ; } public void paserFileToString(LingJoinFile f) throws Exception {
// TODO Auto-generated method stub
f.setlContent(this.getContent(f)) ;
} public PDFExtractor(Integer typeFlag) {
// TODO Auto-generated constructor stub
this.typeFlag = typeFlag ;
} private Integer typeFlag = null ; public Integer getTypeFlag() {
// TODO Auto-generated method stub
return typeFlag;
}
}
java 解析office文件 大全的更多相关文章
- Java解析OFFICE(word,excel,powerpoint)以及PDF的实现方案及开发中的点滴分享
Java解析OFFICE(word,excel,powerpoint)以及PDF的实现方案及开发中的点滴分享 在此,先分享下写此文前的经历与感受,我所有的感觉浓缩到一个字,那就是:"坑&qu ...
- java解析xml文件并输出
使用java解析xml文件,通过dom4j,代码运行前需先导入dom4j架包. ParseXml类代码如下: import java.io.File; import java.util.ArrayLi ...
- 使用Java解析XML文件或XML字符串的例子
转: 使用Java解析XML文件或XML字符串的例子 2017年09月16日 11:36:18 inter_peng 阅读数:4561 标签: JavaXML-Parserdom4j 更多 个人分类: ...
- Java解析JSON文件的方法
http://blog.sina.com.cn/s/blog_628cc2b70101dydc.html java读取文件的方法 http://www.cnblogs.com/lovebread/ar ...
- Java解析xml文件遇到特殊符号&会出现异常的解决方案
文/朱季谦 在一次Java解析xml文件的开发过程中,使用SAX解析时,出现了这样一个异常信息: Error on line 60 of document : 对实体 "xxx" ...
- java解析XML文件
dom4j是一个Java的XML API,类似于jdom,用来读写XML文件的.dom4j是一个非常非常优秀的Java XML API,具有性能优异.功能强大和极端易用使用的特点,同时它也是一个开放源 ...
- java解析properties文件
在自动化测试过程中,经常会有一些公用的属性要配置,以便后面给脚本使用,我们可以选择xml, excel或者json格式来存贮这些数据,但其实java本身就提供了properties类来处理proper ...
- JAVA解析XML文件(DOM,SAX,JDOM,DOM4j附代码实现)
1.解析XML主要有四种方式 1.DOM方式解析XML(与平台无关,JAVA提供,一次性加载XML文件内容,形成树结构,不适用于大文件) 2.SAX方式解析XML(基于事件驱动,逐条解析,适用于只处理 ...
- java实现office文件预览
不知觉就过了这个久了,继上篇java实现文件上传下载后,今天给大家分享一篇java实现的对office文件预览功能. 相信大家在平常的项目中会遇到需要对文件实现预览功能,这里不用下载节省很多事.大家请 ...
随机推荐
- BZOJ1652 [Usaco2006 Feb]Treats for the Cows
蒟蒻许久没做题了,然后连动规方程都写不出了. 参照iwtwiioi大神,这样表示区间貌似更方便. 令f[i, j]表示i到j还没卖出去,则 f[i, j] = max(f[i + 1, j] + v[ ...
- kernel jenkins build script
#!/bin/bash #gcc: site="https://releases.linaro.org" #https://releases.linaro.org/componen ...
- git忽略操作
在使用git的过程中,我们会有一些文件是不需要版本管理的,这时候就需要忽略这些文件 这里暂时只讲我用到的办法 全局忽略 在任意位置建立 .gitignore 文件,在其中写入你要忽略文件的规则(写法自 ...
- 51nod1482
题解: 发现是一个环,而环的题目有一些就是要转化成为链 首先找到一个最高点,中间断开 然后当作一条链来做 代码: #include<cstdio> #include<algorith ...
- POJ 2242 The Circumference of the Circle
做题回顾:用到海伦公式,还有注意数据类型,最好统一 p=(a+b+c)/2; s=sqrt(p*(p-a)*(p-b)*(p-c));//三角形面积,海伦公式 r=a*b*c/(4*s);//这是外接 ...
- ACCESS表的视图
ACCESS表的视图 点击: 发布日期:2007-8-31 6:37:00 进入论坛 表是关系型数据库的基本结构.在Access中,表是一种关系特定主题的数据集合,如产品.供应商等.为 ...
- [转]数据库更新(Update语句)查询
2011-03-27 10:40:11| 分类: Database |举报|字号 订阅原文出自:http://blog.csdn.net/ylnjust02/archive/2005/12/10/54 ...
- input 文件上传
<button class="blueButton fileinput-button" style="width:165px;" @click=" ...
- SWIFT Enumeration(1)
Swift中定义Enumeration跟其它语言挺类似的,看如下定义一个星期的Enumeration enum Day:Int{ case Monday = 1, Tuesday,Wednesday, ...
- [Spring Boot] Spring Boot启动过程源码分析
关于Spring Boot,已经有很多介绍其如何使用的文章了,本文从源代码(基于Spring-boot 1.5.6)的角度来看看Spring Boot的启动过程到底是怎么样的,为何以往纷繁复杂的配置到 ...