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

下载 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>

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值