wold,exexl,pdf等格式文件的内容提取 (转载)

平时对各种文件内容的提取比较麻烦,比如word,excel,pdf等文件,不能使用普通的BufferedReader,BufferedWriter等流的方式读写提取,因为这些文件的格式不是普通的文本,他们有自己定义的格式,必须使用他们自己提供的jar包进行操作,Word,Excel使用的是Apache提供的poi进行操作,当然在操作的过程中要注意一些使用的方法,比如Word,Excel有不同的版本,操作的方式也不同,这里会出现很多问题,在上一篇中我整理了一些,我在操作过程出现的问题,并提供了解决方案,还有提供了本人操作这些文件的源码,下载即可使用。


Word操作类:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.POIXMLTextExtractor;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.hwpf.usermodel.CharacterProperties;
import org.apache.poi.hwpf.usermodel.HWPFList;
import org.apache.poi.hwpf.usermodel.ParagraphProperties;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.junit.Test;


/**
* 提取word内容
*
* @author wangshouhai
* @Version 2014-4-17:下午12:07:04
*/
public class ReadWord {


public static void main(String[] args) {


File file = new File("C:\\Users\\Administrator\\Desktop\\测试文档.docx");
// readWord2003(file);


readWord2007(file);

}


/**
* 支持word-2003
*
* @param args
*/
private static void readWord2003(File file) {
try {
FileInputStream fis = new FileInputStream(file);
// 创建WordExtractor对象
WordExtractor wordExtractor = new WordExtractor(fis);
// 取得所有文本内容
String text = wordExtractor.getText();
System.out.println("readWord2003--------------->"+text);
} catch (Exception e) {
e.printStackTrace();
}
}


@Test
// 支持word-2003
public static void readWordExtractor(File file) {
try {
FileInputStream fis = new FileInputStream(file);
// 创建WordExtractor对象
WordExtractor wordExtractor = new WordExtractor(fis);
// 通过getParagraphText()提取每个段落
String[] paragraph = wordExtractor.getParagraphText();
System.out.println("该Word文件共有" + paragraph.length + "段。");


for (int i = 0; i < paragraph.length; i++) {
System.out.println("readWordExtractor--------------->"+paragraph[i]);
}
} catch (Exception e) {
e.printStackTrace();
}
}


/**
* word 2007解决方案
*
* @param args
*/
@Test
public static void readWord2007(File file) {
try {
// word 2007,读取word中字符
OPCPackage opcPackage = POIXMLDocument.openPackage("D:\\apache-tomcat-6.0.18\\webapps\\GOVWBWeb\\upload\\user\\2014\\04\\18\\08\\193b299f-e8fc-4a32-a7ba-f951beeec1d9");
POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
String text2007 = extractor.getText();
System.out.println("readWord2007--------------->"+text2007);
} catch (Exception e) {
e.printStackTrace();
}
}

}



Excel操作类:

import java.io.File;
import java.io.FileInputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
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;


/**
* 读取excel内容
* @author wangshouhai
* @Version 2014-4-18:下午12:56:23
*/
public class ReadExcel {

// 文件上传
public static void readExcel2007(File file) {


try {
// 创建工作区,读取上传文件
XSSFWorkbook wb= new XSSFWorkbook(new FileInputStream(file));
XSSFSheet sheet =wb.getSheetAt(0);
int rows = sheet.getPhysicalNumberOfRows();// 获取所有的行
if (rows > 0) {
for (int i = 1; i < rows; i++) {
XSSFRow row=sheet.getRow(i);
if (row == null) {
continue;
}
try {
XSSFCell idCell = row.getCell(0);
if (idCell != null) {
double id = idCell.getNumericCellValue();
//int id = Integer.parseInt(idCell.getRichStringCellValue().toString());
System.out.println("id----------->"+id);
}


// 账号
XSSFCell accountsCell = row.getCell(1);
String accounts = null;
if (accountsCell != null) {
accounts = accountsCell.getRichStringCellValue().toString();
System.out.println("accounts----------->"+accounts);
}


// 密码
XSSFCell passwordCell = row.getCell(2);
if (passwordCell != null) {
String password = passwordCell.getRichStringCellValue().toString();
System.out.println("password----------->"+password);
}


// 姓名
XSSFCell nameCell = row.getCell(3);
if (nameCell != null) {
String name = nameCell.getRichStringCellValue().toString();
System.out.println("name----------->"+name);
}


// 性别
XSSFCell sexCell = row.getCell(4);
if (sexCell != null) {
double sex = idCell.getNumericCellValue();
//String sex = sexCell.getRichStringCellValue().toString();
//int sexs = Integer.parseInt(sex);
System.out.println("sex----------->"+sex);
}


// 邮箱
XSSFCell emailCell = row.getCell(5);
if (emailCell != null) {
String email = emailCell.getRichStringCellValue().toString();
System.out.println("email----------->"+email);
}


// 手机
XSSFCell phoneCell = row.getCell(6);
if (phoneCell != null) {
String phone = phoneCell.getRichStringCellValue().toString();
System.out.println("phone----------->"+phone);
}


} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}



public static void readExcel2003(File file) {


try {
// 创建工作区,读取上传文件
Workbook wb = WorkbookFactory.create(new FileInputStream(file));
Sheet sheet = wb.getSheetAt(0);
int rows = sheet.getPhysicalNumberOfRows();// 获取所有的行


if (rows > 0) {
for (int i = 1; i < rows; i++) {
//获取每一行
HSSFRow row = (HSSFRow) sheet.getRow(i);
if (row == null) {
continue;
}
try {
//获取列数开始
HSSFCell idCell = row.getCell(0);
if (idCell != null) {
double id = idCell.getNumericCellValue();
// int id =
// Integer.parseInt(idCell.getRichStringCellValue().toString());
System.out.print("id: "+id+",");
}


// 账号
HSSFCell accountsCell = row.getCell(1);
String accounts = null;
if (accountsCell != null) {
accounts = accountsCell.getRichStringCellValue().toString();
System.out.print("accounts: "+accounts+",");
}


// 密码
HSSFCell passwordCell = row.getCell(2);
if (passwordCell != null) {
String password = passwordCell.getRichStringCellValue().toString();
System.out.print("password: "+password+",");
}


// 姓名
HSSFCell nameCell = row.getCell(3);
if (nameCell != null) {
String name = nameCell.getRichStringCellValue().toString();
System.out.print("name: "+name+",");
}


// 性别
HSSFCell sexCell = row.getCell(4);
if (sexCell != null) {
double sex = idCell.getNumericCellValue();
// String sex =
// sexCell.getRichStringCellValue().toString();
// int sexs = Integer.parseInt(sex);
System.out.print("sex: "+sex+",");
}


// 邮箱
HSSFCell emailCell = row.getCell(5);
if (emailCell != null) {
String email = emailCell.getRichStringCellValue().toString();
System.out.print("email: "+email+",");
}


// 手机
HSSFCell phoneCell = row.getCell(6);
if (phoneCell != null) {
String phone = phoneCell.getRichStringCellValue().toString();
System.out.println("phone: "+phone);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}

/**
* 读取Excel2007
* @param args
*/

public static void main(String[] args) {
File file = new File("D:\\apache-tomcat-6.0.18\\webapps\\GOVWBWeb\\upload\\user\\2014\\04\\18\\11\\adcc6bc6-bd5e-43e9-9a53-3ba879dfa62d.xlsx");
//readExcel2007(file);
readExcel2003(new File("C:\\Users\\Administrator\\Desktop\\export.xls"));
}
}



Pdf操作类:

import java.io.FileInputStream;
import org.apache.pdfbox.cos.COSDocument;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFTextStripper;


/**
* 提取pdf中的内容
* @author wangshouhai
* @Version 2014-4-18:下午12:47:27
*/
public class ReadPdf {
public String readFdf(String file) {
try {
PDFParser parser = new PDFParser(new FileInputStream(file));
parser.parse();
COSDocument doc=parser.getDocument();
PDFTextStripper stripper = new PDFTextStripper();
String docText = stripper.getText(new PDDocument(doc));
docText= convertorSymbol(docText);
return docText;
} catch (Exception e) {
throw new RuntimeException(e);
}
}




/**
* 处理特殊字符
*
* @param sub
* @param docText
*/
public static String convertorSymbol(String docText) {
StringBuilder sub = new StringBuilder();
char[] ch = docText.toCharArray();
for (int i = 0; i < ch.length; i++) {
char buf = ch[i];
if (9 == buf || 10 == buf || 13 == buf || 32 <= buf && !Character.isISOControl(buf)) {
sub.append(buf);
}
}
return sub.toString().replaceAll("\\s*", "");
}

public static void main(String args[]) {
String text =new ReadPdf().readFdf("D:\\html2.pdf");
System.out.println("ReadPdf---------->"+text);
}
}


文本操作类:



import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;


public class ReadText {
/**
* 读取文本内容
*
* @param dataFile
* @return
*/
public static String readText(File file) {
StringBuilder sub = new StringBuilder();
BufferedReader bufReader = null;
try {
bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
String str;
while ((str = bufReader.readLine()) != null) {
sub.append(str);
}
return convertorSymbol(sub.toString());
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
if (bufReader != null) {
try {
bufReader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}

/**
* 处理特殊字符
*
* @param sub
* @param docText
*/
public static String convertorSymbol(String docText) {
StringBuilder sub = new StringBuilder();
char[] ch = docText.toCharArray();
for (int i = 0; i < ch.length; i++) {
char buf = ch[i];
if (9 == buf || 10 == buf || 13 == buf || 32 <= buf && !Character.isISOControl(buf)) {
sub.append(buf);
}
}
return sub.toString();
}



public static void main(String[] args) {
File file = new File("C:/Users/Administrator/Desktop/异常信息列表.txt");
String text = readText(file);
System.out.println("text-->"+text);
}
}
文件上传并提取内容

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值