免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1040 | 回复: 0
打印 上一主题 下一主题

JAVA读取WORD,EXCEL,POWERPOINT,PDF文件的方法【转】 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-10-19 22:38 |只看该作者 |倒序浏览

OFFICE文档使用POI控件,PDF可以使用PDFBOX0.7.3控件,完全支持中文,用XPDF也行,不过感觉PDFBOX比较好,而且作者也在更新。水平有限,万望各位指正
WORD:
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.poi.hwpf.extractor.WordExtractor;
import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream;
import com.search.code.Index;
public Document getDocument(Index index, String url, String title, InputStream is) throws DocCenterException {
  String bodyText = null;
  try {
   WordExtractor ex = new WordExtractor(is);//is是WORD文件的InputStream
   bodyText = ex.getText();
   if(!bodyText.equals("")){
    index.AddIndex(url, title, bodyText);
   }
  }catch (DocCenterException e) {
   throw new DocCenterException("无法从该Mocriosoft Word文档中提取内容", e);
  }catch(Exception e){
   e.printStackTrace();
  }
}
  return null;
}
Excel:
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.poi.hwpf.extractor.WordExtractor;
import  org.apache.poi.hssf.usermodel.HSSFWorkbook;
import  org.apache.poi.hssf.usermodel.HSSFSheet;
import  org.apache.poi.hssf.usermodel.HSSFRow;
import  org.apache.poi.hssf.usermodel.HSSFCell;
import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream;
import com.search.code.Index;
public Document getDocument(Index index, String url, String title, InputStream is) throws DocCenterException {
  StringBuffer content = new StringBuffer();
  try{
   HSSFWorkbook  workbook  =  new  HSSFWorkbook(is);//创建对Excel工作簿文件的引用
   for (int numSheets = 0; numSheets

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/37861/showart_404002.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP