java 读取pdf、word、Excel文件
用到的jar:
itextpdf-5.5.8.jar (PDF)
poi.jar
public class FileUtils {
/**
* 判断文件是否存在
*
* @Title: isExcite
* @param @param filePath
* @param @return
* @return boolean 返回类型
* @throws
*/
public static boolean isExcite(String filePath) {
File file = new File(filePath);
// 如果文件夹不存在则创建
if (!file.exists() && !file.isDirectory()) {
return false;
} else {
return true;
}
}
/**
*
* @Title: getPdfFileText
* @Description: 获取指定位置pdf的文件内容
* @param @param fileName
* @param @return
* @param @throws IOException
* @return String 返回类型
* @throws
*/
public static String getPdfFileText(String fileName) throws IOException {
PdfReader reader = new PdfReader(fileName);
PdfReaderContentParser parser = new PdfReaderContentParser(reader);
StringBuffer buff = new StringBuffer();
TextExtractionStrategy strategy;
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
strategy = parser.processContent(i,
new SimpleTextExtractionStrategy());
buff.append(strategy.getResultantText());
}
return buff.toString();
}
/**
* 获取doc文档
*
* @Title: getTextFromWord
* @param @param filePath
* @param @return
* @return String 返回类型
* @throws
*/
public static String getTextFromWord(String filePath) {
String result = null;
File file = new File(filePath);
try {
FileInputStream fis = new FileInputStream(file);
WordExtractor wordExtractor = new WordExtractor(fis);
result = wordExtractor.getText();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
/**
* 读取excel内容
*
* @Title: getTextFromExcel
* @param @param filePath
* @param @return
* @return String 返回类型
* @throws
*/
public static String getTextFromExcel(String filePath) {
StringBuffer buff = new StringBuffer();
try {
// 创建对Excel工作簿文件的引用
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(filePath));
// 创建对工作表的引用。
for (int numSheets = 0; numSheets < wb.getNumberOfSheets(); numSheets++) {
if (null != wb.getSheetAt(numSheets)) {
HSSFSheet aSheet = wb.getSheetAt(numSheets);// 获得一个sheet
for (int rowNumOfSheet = 0; rowNumOfSheet <= aSheet
.getLastRowNum(); rowNumOfSheet++) {
if (null != aSheet.getRow(rowNumOfSheet)) {
HSSFRow aRow = aSheet.getRow(rowNumOfSheet); // 获得一个行
for (int cellNumOfRow = 0; cellNumOfRow <= aRow
.getLastCellNum(); cellNumOfRow++) {
if (null != aRow.getCell(cellNumOfRow)) {
HSSFCell aCell = aRow.getCell(cellNumOfRow);// 获得列值
switch (aCell.getCellType()) {
case HSSFCell.CELL_TYPE_FORMULA:
break;
case HSSFCell.CELL_TYPE_NUMERIC:
buff
.append(
aCell
.getNumericCellValue())
.append('\t');
break;
case HSSFCell.CELL_TYPE_STRING:
buff.append(aCell.getStringCellValue())
.append('\t');
break;
}
}
}
buff.append('\n');
}
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return buff.toString();
}
/**
* 替换文件内容
* @Title: replaceContentToFile
* @param @param path 文件路径
* @param @param str 要替换的内容
* @param @param con 替换称的内容
* @return void 返回类型
* @throws
*/
public static void replaceContentToFile(String path, String str, String con) {
try {
if (isExcite(path)) {
FileReader read = new FileReader(path);
BufferedReader br = new BufferedReader(read);
StringBuilder content = new StringBuilder();
while (br.ready() != false) {
content.append(br.readLine());
content.append("\r\n");
}
int dex = content.indexOf(str);
if (dex != -1) {
System.out.println("找到标记!");
} else {
System.out.println("指定标记不存在!");
}
content.replace(dex, dex, con);
br.close();
read.close();
FileOutputStream fs = new FileOutputStream(path);
fs.write(content.toString().getBytes());
fs.close();
} else {
System.out.println("文件不存在!");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
留着以后直接拿过来用。
java 读取pdf、word、Excel文件的更多相关文章
- Java解析OFFICE(word,excel,powerpoint)以及PDF的实现方案及开发中的点滴分享
Java解析OFFICE(word,excel,powerpoint)以及PDF的实现方案及开发中的点滴分享 在此,先分享下写此文前的经历与感受,我所有的感觉浓缩到一个字,那就是:"坑&qu ...
- Java中使用POI读取大的Excel文件或者输入流时发生out of memory异常参考解决方案
注意:此参考解决方案只是针对xlsx格式的excel文件! 背景 前一段时间遇到一种情况,服务器经常宕机,而且没有规律性,查看GC日志发生了out of memory,是堆溢出导致的,分析了一下堆的d ...
- java使用POI实现excel文件的读取,兼容后缀名xls和xlsx
需要用的jar包如下: 如果是maven管理的项目,添加依赖如下: <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --&g ...
- java读取各类型的文件
java读取各类型的文件 用到的几个包 bcmail-jdk14-132.jar/bcprov-jdk14-132.jar/checkstyle-all-4.2.jar/FontBox-0.1.0-d ...
- PDF/WORD/EXCEL 图片预览
一.PDF/WORD/EXCEL 转 XPS 转 第一页内容 转 图片 WORD.EXCEL转XPS (Office2010) public bool WordToXPS(string sourceP ...
- java 如何将 word,excel,ppt如何转pdf--jacob
问题:java 如果将 word,excel,ppt如何转pdf 我个人的观点:windows server下用 jacob; linux server下 用openoffice. PS:1.本文 ...
- java导入、导出Excel文件
一.介绍 当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是:我们已经习惯用Excel打印.这样在我们实际 ...
- Java生成和操作Excel文件(转载)
Java生成和操作Excel文件 JAVA EXCEL API:是一开放源码项目,通过它Java开发人员可以读取Excel文件的内容.创建新的Excel文件.更新已经存在的Excel文件.使用该A ...
- c++ 读取并解析excel文件方法
用Cocos开发模型特效工具编辑器,跨Mac和windows,当中有个需求是读取并解析excel文件,但网上的查找的例子几乎都只能是在windows下面使用,再或者是命令行脚本之类的.于是,自己写了一 ...
- java读取pdf文本转换html
补充:一下代码基于maven,现将依赖的jar包单独导出 地址:pdf jar 完整代码地址 也就两个文件 java读取pdf中的纯文字,这里使用的是pdfbox工具包 maven引入如下配置 < ...
随机推荐
- Git: 生成ssh公钥
生成 SSH 公钥 大多数 Git 服务器都会选择使用 SSH 公钥来进行授权.系统中的每个用户都必须提供一个公钥用于授权,没有的话就要生成一个.生成公钥的过程在所有操作系统上都差不多. 首先先确认一 ...
- django models auto_now和auto_now_add的区别
DataTimeField()中auto_now参数和auto_now_add参数区别: 前者添加或者修改的都为现在的时间,可以再次更新: 后者仅仅为添加时候的时间,不可更改.
- Ubuntu学习总结-08 Ubuntu运行Shell脚本报 shell /bin/bash^M: bad interpreter错误问题解决
错误原因之一很有可能是运行的脚本文件是DOS格式的, 即每一行的行尾以\r\n来标识, 其ASCII码分别是0x0D, 0x0A.可以有很多种办法看这个文件是DOS格式的还是UNIX格式的, 还是MA ...
- 从Paxos到ZooKeeper-二、ZooKeeper和Paxos
ZooKeeper为分布式应用提供了高效且可靠的分布式协调服务,提供了诸如tong'yi统一命名服务.配置管理和分布式锁等分布式的基础服务.在解决分布式数据一致性方面,ZooKeeper并没有直接采用 ...
- Ajax Post 类实例
以前总是ajax请求是这样的 data:"a=1&b=2&c=3..." 而Controller也总是这样的 Action(int a,int b,int c) 很 ...
- IIS------配置.Net 4.0
转载: http://blog.csdn.net/mazhaojuan/article/details/7660657
- vim 显示颜色脚本
vim 显示颜色脚本 echo TERM=xterm >>/root/.bash_profile echo export TERM >>/root/.bash_profile ...
- Android学习笔记——Bundle
该工程的功能是实现不同线程之间数据的传递 以下代码是MainActivity.java中的代码 package com.example.bundle; import android.app.Activ ...
- Java Web的两种开发模式
参考文献:http://www.cnblogs.com/xdp-gacl/p/3908610.html 一.Jsp+JavaBean 此模式如下图所示:
- FBX
http://docs.autodesk.com/FBX/2014/ENU/FBX-SDK-Documentation/index.html http://forums.autodesk.com/t5 ...