关于lucene处理word、excel、ppt、txt、pdf等格式文件的代码如下:
public class ReadFile {
/**
* 处理word2003
* @param path
* @return
* @throws Exception
*/
public static String readWord(String path) throws Exception {
String bodyText = null;
InputStream inputStream = new FileInputStream(path);
WordExtractor extractor = new WordExtractor(inputStream);
bodyText = extractor.getText();
return bodyText;
}
/**
* 处理word2007
* @param path
* @return
* @throws IOException
* @throws OpenXML4JException
* @throws XmlException
*/
public static String readWord2007(String path) throws IOException, OpenXML4JException, XmlException {
OPCPackage opcPackage = POIXMLDocument.openPackage(path);
POIXMLTextExtractor ex = new XWPFWordExtractor(opcPackage);
return ex.getText();
}
/**
* 处理excel2003
* @param path
* @return
* @throws IOException
*/
public static String ReadExcel(String path) throws IOException {
InputStream inputStream = null;
String content = null;
try {
inputStream = new FileInputStream(path);
HSSFWorkbook wb = new HSSFWorkbook(inputStream);
ExcelExtractor extractor = new ExcelExtractor(wb);
extractor.setFormulasNotResults(true);
extractor.setIncludeSheetNames(false);
content = extractor.getText();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return content;
}
/**
* 处理excel2007
* @param path
* @return
* @throws IOException
*/
public static String readExcel2007(String path) throws IOException {
StringBuffer content = new StringBuffer();
// 构造 XSSFWorkbook 对象,strPath 传入文件路径
XSSFWorkbook xwb = new XSSFWorkbook(path);
// 循环工作表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();
}
/**
* 处理ppt
* @param path
* @return
*/
public static String readPowerPoint(String path) {
StringBuffer content = new StringBuffer("");
InputStream inputStream = null;
try {
inputStream = new FileInputStream(path);
SlideShow ss = new SlideShow(new HSLFSlideShow(new FileInputStream(path)));// is
// 为文件的InputStream,建立SlideShow
Slide[] slides = ss.getSlides();// 获得每一张幻灯片
for (int i = 0; i < slides.length; i++) {
TextRun[] t = slides[i].getTextRuns();// 为了取得幻灯片的文字内容,建立TextRun
for (int j = 0; j < t.length; j++) {
content.append(t[j].getText());// 这里会将文字内容加到content中去
}
}
} catch (Exception ex) {
System.out.println(ex.toString());
}
return content.toString();
}
/**
* 处理pdf
* @param path
* @return
* @throws IOException
*/
public static String readPdf(String path) throws IOException {
StringBuffer content = new StringBuffer("");// 文档内容
PDDocument pdfDocument = null;
try {
FileInputStream fis = new FileInputStream(path);
PDFTextStripper stripper = new PDFTextStripper();
pdfDocument = PDDocument.load(fis);
StringWriter writer = new StringWriter();
stripper.writeText(pdfDocument, writer);
content.append(writer.getBuffer().toString());
fis.close();
} catch (java.io.IOException e) {
System.err.println("IOException=" + e);
System.exit(1);
} finally {
if (pdfDocument != null) {
org.pdfbox.cos.COSDocument cos = pdfDocument.getDocument();
cos.close();
pdfDocument.close();
}
}
return content.toString();
}
/**
* 处理txt
* @param path
* @return
* @throws IOException
*/
public static String readTxt(String path) throws IOException {
StringBuffer sb = new StringBuffer("");
InputStream is = new FileInputStream(path);
// 必须设置成GBK,否则将出现乱码
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "GBK"));
try {
String line = "";
while ((line = reader.readLine()) != null) {
sb.append(line + "\r");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return sb.toString().trim();
}
}
使用jar包如下:
- 大小: 37.3 KB
分享到:
相关推荐
基于lucene实现word、ppt、excel、pdf、txt全文检索
基本可以满足 检索电脑内文件的需求了 已经封装过了 直接指定检索目录和索引目录来创建索引.提供给学习者门参考... 直接通过调用静态方法来实现 深度分页检索.
Lucenet 建 word pdf office 索引
http://blog.csdn.net/wuyinggui10000/article/category/3173543 我的博客
Lucene.Net 文件检索doc,xls,ppt,txt,pdf文件(实例),对需要文件检索的朋友很有帮助,10分很值的
转载请注明出处:http://blog.csdn.net/dongdong9223/article/details/76273859本文出自【我是干勾鱼的博客】
lucene.net 用于doc,docx,xls,xlsx,ppt,pptx,pdf等文件检索,该程序是将文件中文本提取出来,然后创建检索,效果很好!
首先说明Lucene如果想对office进行操作的话,需要额外的包这里有几种方法一是使用POI这里的代码使用的是jacob对于word进行处理代码:public
lucene全文检索文件!包括对txt,world,excel,ppt,pdf检索!支持office07
lucene教程幻灯片 lucene教程幻灯片
lucene in action 第二版(pdf),内为英文彩色原版。
word2vec(word to vector)是一个将单词转换成向量形式的工具,Lucene 是apache软件基金会一个开放源代码的全文检索引擎工具包,Word2vec-Lucene是用于将Lucene检索到的内容转换成向量形式的工具。 标签:...
搜索引擎 Lucene PPT 教程 超过10章内容
是对lucene原理的描述和实现,学起来简单易用
资源全免费,我的博客有相关的说明,这个是一个eclipse项目,是一个使用lucene全文检索word2007的例子,可以直接运行,但是需要在数据目录下放入word文件 才能创建索引.
lucene.pptlucene.pptlucene.pptlucene.pptlucene.pptlucene.pptlucene.ppt
iTextPDFExtractor.java ------ ----使用iText解析PDF 文档代码 PDFBoxHello.java ----------- --PDFBox测试代码 PDFBoxLuceneIndex.java ------ --PDFBox创建PDF文件的Lucene...-- POI处理Excel和Word文档代码
Lucene学习总结之三:Lucene的索引文件格式(1) Lucene学习总结之三:Lucene的索引文件格式(2) Lucene学习总结之三:Lucene的索引文件格式(3) Lucene学习总结之四:Lucene索引过程分析(1) Lucene学习总结之...
lucene 搜索中文文档,写的比较详细
lucene 是全开源文索引搜索引擎,使用它可以很快的架设我们自己的搜索引擎,但遗憾的是它默认只对txt和html格式文档生成index,但我们常常要使用其他的文档如word,pdf等,我搜集了这几种文档生成index的笔记