java 处理word,excel,pdf -javacode
2008年08月27日 星期三 01:08 P.M.

java 处理word,excel,pdf -javacode

很多人问到如何抽取word,excel,pdf阿。这里我总结一下抽取word,pdf的
几种方法。
1。用jacob.
其实jacob是一个bridage,连接java和com或者win32函数的一个中间件,jacob并不能直接抽取word,excel等文件,需要自己写dll哦,不过已经有为你写好的了,就是jacob的作者一并提供了。
jacob下载:
http://www.java-cn.com/technology/tech_downs/1880_001.zip
下载了jacob并放到指定的路径之后(dll放到path,jar文件放到classpath),就可以写你自己的抽取程序了,下面是一个例子:
import java.io.File;
import com.jacob.com.*;
import com.jacob.activeX.*;

public class FileExtracter{

public static void main(String[] args) {

ActiveXComponent app = new ActiveXComponent("Word.Application");
String inFile = "c: est.doc";
String tpFile = "c: emp.htm";
String otFile = "c: emp.xml";
boolean flag = false;
try {
app.setProperty("Visible", new Variant(false));
Object docs = app.getProperty("document.").toDispatch();
Object doc = Dispatch.invoke(docs,"Open", Dispatch.Method, new Object[]{inFile,new Variant(false), new Variant(true)}, new int[1]).toDispatch();
Dispatch.invoke(doc,"SaveAs", Dispatch.Method, new Object[]{tpFile,new Variant(8)}, new int[1]);
Variant f = new Variant(false);
Dispatch.call(doc, "Close", f);
flag = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[] {});
}

}
}
2。用apache的poi来抽取word,excel。
poi是apache的一个项目,不过就算用poi你可能都觉得很烦,不过不要紧,这里提供了更加简单的一个接口给你:
下载经过封装后的poi包:
http://www.java-cn.com/technology/tech_downs/1880_002.zip
下载之后,放到你的classpath就可以了,下面是如何使用它的一个例子:
import java.io.*;
import org.textmining.text.extraction.WordExtractor;
/**
* <p>Title: pdf extraction</p>
* <p>Description: email:chris@matrix.org.cn</p>
* <p>Copyright: Matrix Copyright (c) 2003</p>
* <p>Company: Matrix.org.cn</p>
* @author chris
* @version 1.0,who use this example pls remain the declare
*/

public class PdfExtractor {
public PdfExtractor() {
}
public static void main(String args[]) throws Exception
{
FileInputStream in = new FileInputStream ("c:a.doc");
WordExtractor extractor = new WordExtractor();
String str = extractor.extractText(in);
System.out.println("the result length is"+str.length());
System.out.println("the result is"+str);
}
}

3。pdfbox-用来抽取pdf文件
但是pdfbox对中文支持还不好,先下载pdfbox:
http://www.java-cn.com/technology/tech_downs/1880_003.zip
下面是一个如何使用pdfbox抽取pdf文件的例子:
import org.pdfbox.pdmodel.PDdocument.
import org.pdfbox.pdfparser.PDFParser;
import java.io.*;
import org.pdfbox.util.PDFTextStripper;
import java.util.Date;
/**
* <p>Title: pdf extraction</p>
* <p>Description: email:chris@matrix.org.cn</p>
* <p>Copyright: Matrix Copyright (c) 2003</p>
* <p>Company: Matrix.org.cn</p>
* @author chris
* @version 1.0,who use this example pls remain the declare
*/

public class PdfExtracter{

public PdfExtracter(){
}
public String GetTextFromPdf(String filename) throws Exception
{
String temp=null;
PDdocument.nbsppdfdocument.null;
FileInputStream is=new FileInputStream(filename);
PDFParser parser = new PDFParser( is );
parser.parse();
pdfdocument.nbsp= parser.getPDdocument.);
ByteArrayOutputStream out = new ByteArrayOutputStream();
OutputStreamWriter writer = new OutputStreamWriter( out );
PDFTextStripper stripper = new PDFTextStripper();
stripper.writeText(pdfdocument.getdocument.), writer );
writer.close();
byte[] contents = out.toByteArray();

String ts=new String(contents);
System.out.println("the string length is"+contents.length+" ");
return ts;
}
public static void main(String args[])
{
PdfExtracter pf=new PdfExtracter();
PDdocument.nbsppdfdocument.nbsp= null;

try{
String ts=pf.GetTextFromPdf("c:a.pdf");
System.out.println(ts);
}
catch(Exception e)
{
e.printStackTrace();
}
}

}

4.抽取支持中文的pdf文件-xpdf
xpdf是一个开源项目,我们可以调用他的本地方法来实现抽取中文pdf文件。
下载xpdf函数包:
http://www.java-cn.com/technology/tech_downs/1880_004.zip
同时需要下载支持中文的补丁包:
http://www.java-cn.com/technology/tech_downs/1880_005.zip
按照readme放好中文的patch,就可以开始写调用本地方法的java程序了
下面是一个如何调用的例子:
import java.io.*;
/**
* <p>Title: pdf extraction</p>
* <p>Description: email:chris@matrix.org.cn</p>
* <p>Copyright: Matrix Copyright (c) 2003</p>
* <p>Company: Matrix.org.cn</p>
* @author chris
* @version 1.0,who use this example pls remain the declare
*/

public class PdfWin {
public PdfWin() {
}
public static void main(String args[]) throws Exception
{
String PATH_TO_XPDF="C:Program Filesxpdfpdftotext.exe";
String filename="c:a.pdf";
String[] cmd = new String[] { PATH_TO_XPDF, "-enc", "UTF-8", "-q", filename, "-"};
Process p = Runtime.getRuntime().exec(cmd);
BufferedInputStream bis = new BufferedInputStream(p.getInputStream());
InputStreamReader reader = new InputStreamReader(bis, "UTF-8");
StringWriter out = new StringWriter();
char [] buf = new char[10000];
int len;
while((len = reader.read(buf))>= 0) {
//out.write(buf, 0, len);
System.out.println("the length is"+len);
}
reader.close();
String ts=new String(buf);
System.out.println("the str is"+ts);
}
}

http://hi.baidu.com/stq1102/blog

http://hi.baidu.com/xuecj/blog/item/37b5938b6f1b75d6fd1f103e.html

java 处理word,excel,pdf -javacode相关推荐

  1. txt doc rtf html,JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例.docx

    JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例 JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例??2012-06-2 ...

  2. java操作word/excel/pdf等文件技术方案

    最近项目中遇到很多对word/excel/pdf等文件的操作,解决方案有好多,开源免费有:利用openoffice组件(需要安装openoffice软件),poi,itext等.也有收费的服务:asp ...

  3. Java处理Word, Excel, PDF文档的4种开源系统的代码例子

    原文: http://blog.csdn.net/wzwfly/article/details/1645046   很多人用java进行文档操作时经常会遇到一个问题,就是如何获得word,excel, ...

  4. java poi- 实现 word Excel pdf ppt 转 HTML

    所需要 jar poi-3.17.jar poi-examples-3.17.jar poi-excelant-3.17.jar poi-ooxml-3.17.jar poi-ooxml-schema ...

  5. java全文检索word中的内容_搜索引擎时对WORD,EXCEL,PDF,POWERPOINT文件全文检索的总结...

    搜索引擎时对WORD,EXCEL,PDF,POWERPOINT文件全文检索的总结 (2012-02-12 16:31:59) 标签: pdf文档 幻灯片 pdf文件 全文检索 控件 it JAVA读取 ...

  6. [JAVA使用技巧]Java抽取Word和PDF格式文件_网络大本营

    Java抽取Word和PDF格式文件的四种武器(1) 很多人用java进行文档操作时经常会遇到一个问题,就是如何获得word,excel,pdf等文档的内容?我研究了一下,在这里总结一下抽取word, ...

  7. Aspose.Java实现word转pdf,添加水印等操作

    Aspose.Java实现word转pdf,添加水印等操作 一. word转pdf 二. 文档插入水印 Aspose是一款商用版控件,支持各类文档操作,这里主要介绍如何在Springboot项目中使用 ...

  8. Web方式预览Office/Word/Excel/pdf文件解决方案

    Web方式预览Office/Word/Excel/pdf文件解决方案 参考文章: (1)Web方式预览Office/Word/Excel/pdf文件解决方案 (2)https://www.cnblog ...

  9. java实现word、pdf文件下载功能

    在SpringMVC的开发过程中,有时需要实现文档的下载功能.文档的下载功能涉及到了java IO流操作的基础知识,下面本文详细介绍java如何实现后台文档下载功能. 首先根据文档在项目中的存储路径建 ...

  10. java 模板 word转pdf 可分页 带图片

    java 模板 word转pdf 可分页 带图片 之前写过一个简单的案例,但是在项目中完全不能满足客户的需求,所以重新用啦一种方式来写,采用了word转换pdf的方式,这种经过不断研究,满足了可分页, ...

最新文章

  1. android groovy 注解,Groovy 注释
  2. python yield遍历目录
  3. python中字符串中文乱码_Python字符串开头的bquot;、uquot;、rquot;与中文乱码
  4. 关于tomcat出现闪退问题
  5. 晨读,难道只是为了完成任务而读的吗?
  6. SicilyFunny Game
  7. java 分布式rpc框架_分布式RPC框架Apache Dubbo(一)
  8. MySQL笔记-InnoDB物理及逻辑存储结构
  9. nn.Conv2d(nc, ndf, 4, 2, 1, bias=False),nc、ndf是什么?
  10. Posftix邮箱服务
  11. 分公司网络建设---Juniper 设备策略路由配置
  12. sqlserver随机取记录
  13. python植物大战僵尸代码写完了怎么运行_植物大战僵尸的代码如何使用python来实现...
  14. 在有位图索引的表上进行DML操作与enq: TX - row lock contention等待事件问题分析
  15. 旅游B2B2C系统解决方案
  16. Mysql 地区经纬度 查询
  17. vmware扩展磁盘分区
  18. 排序算法(5) -- 快速排序
  19. yolov3运行及保存检测视频(包括摄像头)
  20. itol绘制高颜值的进化树

热门文章

  1. 抖音视频限流了怎么解决,如何才能解决限流问题
  2. 5-6 人生的不同阶段
  3. 微信小程序电商实战—环境搭建篇
  4. 利用python快速转换GenBank和RefSeq的染色体号
  5. 手机端页面自适应解决方案—rem布局
  6. linux ubuntu环境下 android jdk sdk eclipse adt 以及手机连接无法识别的解决方法
  7. maven+ssm+redis配置demo
  8. 李日学撤回私有化要约:寺库市值跌至1783万美元 趣店损失惨重
  9. 工行连接深圳通 dll调不到
  10. mean shift应用_使用Google地图制作MEAN应用(第二部分)