• Java-jacob-文件转HTML


    Java-jacob-文件转HTML:

    下载jacob的jar包,然后举个例子。

    	public static final int WORD_HTML = 8;
    
    	public static final int WORD_TXT = 7;
    
    	public static final int EXCEL_HTML = 44;
    
    	/**
    	 * WORD转HTML
    	 * 
    	 * @param docfile
    	 *            WORD文件全路径
    	 * @param htmlfile
    	 *            转换后HTML存放路径
    	 */
    	public static void wordToHtml(String docfile, String htmlfile) {
    		ActiveXComponent app = new ActiveXComponent("Word.Application"); // 启动word
    		try {
    			app.setProperty("Visible", new Variant(false));
    			Dispatch docs = app.getProperty("Documents").toDispatch();
    			Dispatch doc = Dispatch.invoke(
    					docs,
    					"Open",
    					Dispatch.Method,
    					new Object[] { docfile, new Variant(false),
    							new Variant(true) }, new int[1]).toDispatch();
    			Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {
    					htmlfile, new Variant(WORD_HTML) }, new int[1]);
    			Variant f = new Variant(false);
    			Dispatch.call(doc, "Close", f);
    		} catch (Exception e) {
    			e.printStackTrace();
    		} finally {
    			app.invoke("Quit", new Variant[] {});
    		}
    	}
    
    	/**
    	 * EXCEL转HTML
    	 * 
    	 * @param xlsfile
    	 *            EXCEL文件全路径
    	 * @param htmlfile
    	 *            转换后HTML存放路径
    	 */
    	public static void excelToHtml(String xlsfile, String htmlfile) {
    		ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 启动word
    		try {
    			app.setProperty("Visible", new Variant(false));
    			Dispatch excels = app.getProperty("Workbooks").toDispatch();
    			Dispatch excel = Dispatch.invoke(
    					excels,
    					"Open",
    					Dispatch.Method,
    					new Object[] { xlsfile, new Variant(false),
    							new Variant(true) }, new int[1]).toDispatch();
    			Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[] {
    					htmlfile, new Variant(EXCEL_HTML) }, new int[1]);
    			Variant f = new Variant(false);
    			Dispatch.call(excel, "Close", f);
    		} catch (Exception e) {
    			e.printStackTrace();
    		} finally {
    			app.invoke("Quit", new Variant[] {});
    		}
    	}
    
    	public static void main(String[] args) {
    		excelToHtml(
    				"D:\work\apache-tomcat-6.0.36\webapps\ReportSystem\upload\1374758916167.xls",
    				"D:\work\apache-tomcat-6.0.36\webapps\ReportSystem\upload\1374758916167.html");
    	}
    

    Jacob把Word Excel PPT转成Html

     首先,必须将jacob-1.15-M3-x86.dll这个文件(视版本而定,在32位xp系统中使用该文件,在64位系统中,如win7,就得使用该文件jacob-1.15-M3-x64.dll了)拷贝到C:Program FilesJavajdk1.6.0_04jrein的目录下,然后将jacob.jar在添加到C:Program FilesApache Software FoundationTomcat 6.0lib的目录下,在项目中导入jacob.jar即可使用下面程序:

     

    1.wordhtml

    Word转起来还是比较容易的,不会出什么问题,转换之后进程自动关闭。

     

    public class Word2Html {

       public static void change(String filename, String htmlFilename) {

           ActiveXComponent xl = new ActiveXComponent("Word.Application");

    //打开一个word,不显示窗口      

          try {

               Dispatch.put(xl, "Visible", new Variant(false));

               Object workbooks = xl.getProperty("Documents").toDispatch();

               Object workbook = Dispatch.call((Dispatch) workbooks, "Open",

                      filename).toDispatch();

               Dispatch.invoke((Dispatch) workbook, "SaveAs", Dispatch.Method,

                     new Object[] { htmlFilename, new Variant(8) }, new int[1]);

               Variant f = new Variant(false);

               //Close关闭文件,不关闭窗口

               Dispatch.call((Dispatch) workbooks, "Close", f);

           } catch (Exception e) {

               e.printStackTrace();

           } finally {

               // 调用office关闭方法,关闭窗口和word进程         

               xl.invoke("Quit", new Variant[] {});                 

               xl = null;

           }

        }

       

       public static void main(String[] args) {

           Word2Html.change("d:/a.doc", "d:/a");

        }

     

    }

    因为word转html之后,word进程自动关闭,所以在finally中不写关闭进程代码,但在excel和ppt转换后进程不会自动关闭,要加上关闭进程代码。

     

    2.Excelhtml

     

    Excel最头疼的就是转换完之后,进程不会释放,而且每一次转换都会加一个进程,崩溃!!

    无奈之下不得不在finally中杀掉excel进程。

     

    public class Excel2Html {

       public static void change(String filename, String htmlFilename) {

           ActiveXComponent xl = new ActiveXComponent("Excel.Application");

          try {

               Dispatch.put(xl, "Visible", new Variant(false));

    //打开一个Excel,不显示窗口

               Object workbooks = xl.getProperty("workbooks").toDispatch();

                            Object workbook = Dispatch.call((Dispatch) workbooks, "Open",

                      filename).toDispatch();

               Dispatch.invoke((Dispatch) workbook, "SaveAs", Dispatch.Method,

                     new Object[] { htmlFilename, new Variant(44) }, new int[1]);

               Dispatch.call((Dispatch) workbooks, "Close");

           } catch (Exception e) {

               e.printStackTrace();

           } finally {

               xl.invoke("Quit", new Variant[] {});

               xl = null;

               Process process;

              int pid = 0;

              try {

                  process = Runtime.getRuntime().exec("tasklist");

                  Scanner in = new Scanner(process.getInputStream());

                 while (in.hasNextLine()) {

                      String p = in.nextLine();

                      // 打印所有进程

                      System.out.println(p);

                     if (p.contains("EXCEL.EXE")) {

                         StringBuffer buf = new StringBuffer();

                        for (int i = 0; i < p.length(); i++) {

                            char ch = p.charAt(i);

                            if (ch != ' ') {

                                buf.append(ch);

                             }

                         }

                         // 打印pid,根据pid关闭进程

                         System.out.println(buf.toString().split("Console")[0]

                                .substring("EXCEL.EXE".length()));

                         pid = Integer.parseInt(buf.toString().split("Console")[0]

                                .substring("EXCEL.EXE".length()));

                         Runtime.getRuntime().exec("tskill"+" "+pid);

                      }

                  }

               } catch (IOException e) {

                  e.printStackTrace();

               }

           }

        }

       

       public static void main(String[] args) {

           Excel2Html.change("d:/b.xls", "d:/b");

        }

    }

     

    3.PPT转html

     

    PPT转html最头疼的就是new Variant(false)会报错,无奈只能设成true,然后在finally中杀掉powerpnt进程。

     

    public class Ppt2Html {

       public static void change(String filename, String htmlFilename) {

           ActiveXComponent xl = new ActiveXComponent("Powerpoint.Application");

          try {

               Dispatch.put(xl, "Visible", new Variant(true));

    //打开一个PPT,显示窗口,PPT的不显示就会报错,狂晕!

               Object workbooks = xl.getProperty("Presentations").toDispatch();

               Object workbook = Dispatch.call((Dispatch) workbooks, "Open",

                      filename).toDispatch();

               Dispatch.invoke((Dispatch) workbook, "SaveAs", Dispatch.Method,

                     new Object[] { htmlFilename, new Variant(20) }, new int[1]);

               //Variant f = new Variant(false);

               //Dispatch.call((Dispatch) workbooks, "Close",f);

               //PPT的加这两行会报错,干脆注释上,反正在下面也关闭进程

           } catch (Exception e) {

               e.printStackTrace();

           } finally {

               xl.invoke("Quit", new Variant[] {});

               xl = null;

               Process process;

              int pid = 0;

              try {

                  process = Runtime.getRuntime().exec("tasklist");

                  Scanner in = new Scanner(process.getInputStream());

                 while (in.hasNextLine()) {

                      String p = in.nextLine();

                      // 打印所有进程

                      System.out.println(p);

                     if (p.contains("POWERPNT.EXE")) {

                         StringBuffer buf = new StringBuffer();

                        for (int i = 0; i < p.length(); i++) {

                            char ch = p.charAt(i);

                            if (ch != ' ') {

                                buf.append(ch);

                             }

                         }

                         // 打印pid,根据pid关闭进程

                         System.out.println(buf.toString().split("Console")[0]

                                .substring("POWERPNT.EXE".length()));

                         pid = Integer

                                .parseInt(buf.toString().split("Console")[0]

                                       .substring("POWERPNT.EXE".length()));

                         Runtime.getRuntime().exec("tskill" + " " + pid);

                      }

                  }

               } catch (IOException e) {

                  e.printStackTrace();

               }

           }

        }

     

       public static void main(String[] args) {

           Ppt2Html.change("d:/c.ppt", "d:/c");

        }

    }

  • 相关阅读:
    16 Errors and Exceptions
    13 Asynchronous Programming
    wpf入门
    iframe cross domain
    ini_set("error_log",$error_path)设置
    MySQL中merge表存储引擎用法
    php中静态属性静态方法的用法
    PHP的SESSIONID生成原理
    composer的基本 使用
    相邻省份数组
  • 原文地址:https://www.cnblogs.com/Alandre/p/3221006.html
Copyright © 2020-2023  润新知