下载 jacob-1.15-M3

地址http://sourceforge.net/projects/jacob-project/

开发工具 MyEclipse8.0 绿色版、Tomcat 6.0 绿色版,JDK 1.6,XP系统,J2EE项目

在MyEclipse中配置Tomcat,使用自己安装的JDK

接着配置MyEclipse 的JRE

Window---Preferences--Java---Installed JREs--选择之前配置Tomcat时加入的jdk

把jacob-1.15-M3-x86.dll 放到这目录下

C:/Program Files/Java/jdk1.6.0_10/jre/bin

把jacob.jar 放到这目录下

D:/tomcat6/lib

Jacob2Html.java 这个类是在网上找的,谁写了,我记不得了。

package jacob;

//import org.apache.commons.logging.Log;
//import org.apache.commons.logging.LogFactory;
//import com.hz.dao.NoticeDAO;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class Jacob2Html
{
// private static final Log log = LogFactory.getLog(Jacob2Html.class);
 /** 
     * PowerPoint转成HTML 
     * @param pptPath PowerPoint文件全路径 
     * @param htmlfile 转换后HTML存放路径 
    */ 
    public static void pptToHtml(String pptPath, String htmlPath)  
    {  
     ActiveXComponent offCom = new ActiveXComponent("PowerPoint.Application");
        try 
        {  
         offCom.setProperty("Visible", new Variant(true));  
            Dispatch excels = offCom.getProperty("Presentations").toDispatch();  
            Dispatch excel = Dispatch.invoke(excels,
             "Open",Dispatch.Method, new Object[] { pptPath, new Variant(false),  new Variant(false) }, new int[1]).toDispatch();  
            Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[] {  
              htmlPath, new Variant(12) }, new int[1]);  
            Variant f = new Variant(false);  
            Dispatch.call(excel, "Close");  
        }  
        catch (Exception e)  
        {  
            e.printStackTrace();  
        }  
        finally 
        {  
         offCom.invoke("Quit", new Variant[] {});  
        }  
    }
   
  
 /** 
     * WORD转成HTML 
     * @param wordPath WORD文件全路径 
     * @param htmlPath 生成的HTML存放路径 
     */ 
    public static void wordToHtml(String wordPath, String htmlPath)  
    {
     ActiveXComponent offCom = new ActiveXComponent("Word.Application");
        try 
        {  
         offCom.setProperty("Visible", new Variant(false));  
            Dispatch wordDis = offCom.getProperty("Documents").toDispatch();  
            Dispatch doc = Dispatch.invoke(wordDis,  
                    "Open", Dispatch.Method,  new Object[] { wordPath, new Variant(false),  
                            new Variant(true) }, new int[1]).toDispatch();  
            Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {  
              htmlPath, new Variant(8) }, new int[1]);  
            Variant f = new Variant(false);  
            Dispatch.call(doc, "Close", f);  
        }  
        catch (Exception e)  
        {  
            e.printStackTrace();  
        }  
        finally 
        {  
         offCom.invoke("Quit", new Variant[] {});  
        }  
    }  
 
    /** 
     * EXCEL转成HTML 
     * @param xlsfile EXCEL文件全路径 
     * @param htmlfile 转换后HTML存放路径 
     */ 
    public static void excelToHtml(String excelPath, String htmlPath)  
    {  
        ActiveXComponent offCom = new ActiveXComponent("Excel.Application");
        try 
        {  
         offCom.setProperty("Visible", new Variant(false));  
            Dispatch excels = offCom.getProperty("Workbooks").toDispatch();  
            Dispatch excel = Dispatch.invoke(excels,
             "Open",Dispatch.Method, new Object[] { excelPath, new Variant(false),  new Variant(true) }, new int[1]).toDispatch();  
            Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[] {  
              htmlPath, new Variant(44) }, new int[1]);  
            Variant f = new Variant(false);  
            Dispatch.call(excel, "Close", f);  
        }  
        catch (Exception e)  
        {  
            e.printStackTrace();  
        }  
        finally 
        {  
         offCom.invoke("Quit", new Variant[] {});  
        }  
    }   
   
   
    public static void main(String[] args)
 {
     //excelToHtml("c:/ftc/kk.xls","c:/ftc/kk.html");
     //wordToHtml("c:/ftc/gdf.doc","c:/ftc/gdf.html");
     pptToHtml("d:/ch.ppt", "d:/ch.html");
 }
}

///

//InsFile.java

package servlet;

public class InsFile {
 /**
  * 取得文件的后缀名
  * @param filename 文件名
  * @return
  */
 public static String getExt(String filename){
  int index=filename.indexOf(".");
  String ext=filename.substring(index+1);
  String str="";
  if(ext.equalsIgnoreCase("doc"))
   str="doc";
  else if(ext.equalsIgnoreCase("ppt"))
   str="ppt";
  else if(ext.equalsIgnoreCase("xls"))
   str="xls";
  else if(ext.equalsIgnoreCase("pdf"))
   str="pdf";
  else if(ext.equalsIgnoreCase("txt"))
   str="txt";
  return str;
 }
 /**
  * 去掉文件的后缀名

* @param filename 文件名
  * @return
  */
 public static String delExt(String filename){
  int index=filename.indexOf(".");
  String str=filename.substring(0, index);
  return str;
 }
 public static void main(String [] args){
  String s="adfasdf.ppt";
  String e=getExt(s);
  System.out.println("后缀名是:"+e);
  System.out.println("去掉后缀名是:"+delExt(s));
 }
  
}

//

//ReadFileServlet .java

package servlet;
import jacob.Jacob2Html;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;

public class ReadFileServlet extends HttpServlet {

/**
  * Constructor of the object.
  */
 public ReadFileServlet() {
  super();
 }

/**
  * Destruction of the servlet. <br>
  */
 public void destroy() {
  super.destroy(); // Just puts "destroy" string in log
  // Put your code here
 }

/**
  * The doGet method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to get.
  *
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  String tomcat=getServletContext().getRealPath("/")+"upload//";//在工程目录下建个upload文件夹
  String filename=request.getParameter("url");
  
  System.out.println(tomcat);
  System.out.println(filename);
  
  String ext=InsFile.getExt(filename);
  
  File file=new File(tomcat+InsFile.delExt(filename)+".html");
  boolean exist=file.exists();
  if(exist){
   RequestDispatcher   requestDispatcher=request.getRequestDispatcher("/upload/"+InsFile.delExt(filename)+".html");  
   requestDispatcher.forward(request,response);
  }else{
   if(ext.equalsIgnoreCase("doc")){
    Jacob2Html.wordToHtml(tomcat+filename,tomcat+InsFile.delExt(filename)+".html"); 
   }else if(ext.equalsIgnoreCase("ppt")){
    Jacob2Html.pptToHtml(tomcat+filename,tomcat+InsFile.delExt(filename)+".html"); 
System.out.println(tomcat+InsFile.delExt(filename)+".html");
   }else if(ext.equalsIgnoreCase("xls")){
    Jacob2Html.excelToHtml(tomcat+filename,tomcat+InsFile.delExt(filename)+".html"); 
   }
   RequestDispatcher   requestDispatcher=request.getRequestDispatcher("/upload/"+InsFile.delExt(filename)+".html");  
   requestDispatcher.forward(request,response);
  }

}

/**
  * The doPost method of the servlet. <br>
  *
  * This method is called when a form has its tag value method equals to post.
  *
  * @param request the request send by the client to the server
  * @param response the response send by the server to the client
  * @throws ServletException if an error occurred
  * @throws IOException if an error occurred
  */
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  doGet(request, response);
 }

/**
  * Initialization of the servlet. <br>
  *
  * @throws ServletException if an error occurs
  */
 public void init() throws ServletException {
  // Put your code here
 }

}

//在文件夹upload中放置一个123.doc文件

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'index.jsp' starting page</title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
  </head>
 
  <body>
    <a href="./servlet/ReadFileServlet?url=123.doc" target="_blank">This is my JSP page. </a><br>
  </body>
</html>

采用jacob读取并在网页中显示ppt、word、excel相关推荐

  1. 在网页中显示PPT、Word、Excel

    利用在线版微软office套件 先将你的PPT上传为网络路径,通过iframe进行嵌入 <iframe id="iframe1" width="800" ...

  2. Java读取mapinfo格式_求助:如何把mapinfo格式的地图数据放到网页中显示

    声明,我可不是做广告哦 mapXtreme Java,是目前用于Internet或企业Intranet唯一的100%纯Java地图服务器.mapInfo mapXtreme Java与J2EE兼容,为 ...

  3. 在网页中显示CHM (c# csharp .net asp.net winform)

    CHM即"已编译的帮助文件",主要由.hhc(目录文件)..hhk(索引文件)以及相应的帮助主题文件(.html,.htm)这些内容编译而成. 方法对比 在网页中显示CHM内容,大 ...

  4. 在网页中显示数学公式

    网上在网页中显示数学公式的办法有很多,在实验了很多方法后我还是选择两种最简单粗暴的方法. 1.截图 2.ASCIIMathML       比起MathML,ASCIIMathML简单了很多,不需要再 ...

  5. 将VS2005中的.CS文件在网页中显示的方法

    今天在做AccessMembershipProvider的DEMO时,想把支持Access数据库提供程序的源码在网页中显示出来,可是复制源码后在网页中显示的是不换行的,后来我在尝试了很多办法后,终于有 ...

  6. html网页中显示乱码的问题解决

    在网页中显示中文乱码. 情景: 在html页面写了个javacript的代码,里面有alert("中文"),但弹窗出来的对话框显示的是中文乱码. 解决: 在head头加 <m ...

  7. 非常好的在网页中显示pdf的方法

    今天有一需求,要在网页中显示pdf,于是立马开始搜索解决方案,无意中发现一个非常好的解决方法,详见http://blogs.adobe.com/pdfdevjunkie/web_designers_g ...

  8. 网页中显示xml,直接显示xml格式的文件

    第一种方法 使用<pre></pre>包围代码(在浏览器中测试不行啊,但是在富编辑器中又可以,怪): 使用<xmp></xmp>包围代码(官方不推荐,但 ...

  9. html显示docx,网页中显示PDF的HTML代码.docx

    网页中显示PDF的HTML代码 您可能想打开文档至某一特别页面或目标,或显示书签.要与 链接一起放入一个动作命令,可紧接在 PDF 文件名后面键入数字符号 (#) 和该命令.以下表格显示可能的动作命令 ...

最新文章

  1. TCP连接的状态转换图深度剖析
  2. 全球比特币和区块链领域创业企业全景图
  3. c# 字符串排序 (面试题)
  4. Android studio3.1卡顿严重
  5. metasploit framework
  6. java面试题二十五 构造函数
  7. javascript语法速查表
  8. java ArrayList的实现
  9. 数据结构之图的应用:有向无环图
  10. linux系统mysql密码忘记了怎么办_Linux系统下忘记MySql密码怎么办
  11. 集群负载均衡之lvs和keepalived
  12. 入门系列之使用fail2ban防御SSH服务器的暴力破解攻击 1
  13. 计算机二级做的完吗,大学计算机二级都没过 毕业后却做起了IT。。。。。
  14. expect+shell脚本实现免密登录
  15. 线性规划问题的模型建立与求解
  16. sersync同步备份生产实例
  17. 2 月份看了 17 本书,我只推荐这 8 本!
  18. Ant 下载、安装、使用、教程全面了解
  19. Lambda表达式的省略
  20. 有关chromium浏览器 ios 读证书 验证证书 (注没有找到方法),在这里做一下笔记

热门文章

  1. 真无线蓝牙耳机选购小技巧!2020五款优秀低延迟蓝牙耳机推荐
  2. Android逆向系列(一):初探Android逆向
  3. node 压缩图片_用图压批量压缩图片就这么简单
  4. ISP(图像信号处理)之——坏点校正
  5. 【翻译】Visual Place Recognition_ A Survey视觉场景识别综述【三】
  6. 自用大智慧策略高端版本
  7. 图形界面介绍Create Size Blockage
  8. 2020最新部编人教小学语文状元大课堂课件教案习题全套
  9. linux常用命令(包含系统命令)
  10. IOS开发 当滑动tabelview时,使键盘滑落