⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dealword.java

📁 JSP开发的学习系统
💻 JAVA
字号:
package grad.util;
/**
 *   jacob实例操作类
 */

import java.io.File;
import java.io.*;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class dealWord {

 public static boolean convert(File doc,File toFile){
  ActiveXComponent comp = new ActiveXComponent("Word.Application");
  boolean bFlag = true;
  System.out.println(doc.getAbsolutePath());
  try {
   //set Word hidden
   comp.setProperty("Visible", new Variant(false));

   Object wordDis = comp.getProperty("Documents").toDispatch();

   //do MS$Word Open
   Object wordDoc = Dispatch.invoke((Dispatch)wordDis,"Open",Dispatch.Method,
          new Object[] { doc.getAbsolutePath(),
          new Variant(false),new Variant(true) },
          new int[1]).toDispatch();

   //do MS$Word SaveAs
   System.out.println(toFile.getAbsolutePath());
   Dispatch.invoke((Dispatch)wordDoc,"SaveAs",Dispatch.Method,
                 new Object[]{toFile.getAbsolutePath(),
           new Variant(8)},new int[1]);

   //do MS$Word Close
   Dispatch.call((Dispatch)wordDoc, "Close", new Variant(false));

  } catch (Exception e) {
   e.printStackTrace();
   bFlag = false;
  }
  finally{
   comp.invoke("Quit",new Variant[]{});
  }
  return bFlag;
 }
 public static void appendCon(String filename)
 {
	 PrintWriter printer=null;

	 try{

			printer = new PrintWriter(new FileWriter(filename+"bak" ,false),true);
		}catch(java.io.IOException e)
		{

		}

		try{
				BufferedReader in = new BufferedReader(new FileReader(filename));
				String sLine = in.readLine();
				 while(sLine != null){
			 	    if(sLine.indexOf("<body")>=0)
			 	    {
			 	        String tmp="";
			 	    	int pos=sLine.indexOf("<body");
			 	        tmp=sLine.substring(0,pos+5)+" oncontextmenu=self.event.returnValue=false onselectstart=\"return false\" "+
			 	       sLine.substring(pos+5);
			 	       sLine=tmp;
			 	    }
			 	     printer.println(sLine);
					sLine=in.readLine();
			     }

				 printer.close();
				 in.close();
				 File f1=new File(filename);
				 File f2=new File(filename+"bak");
				 //File f3=new File(filename);
				 f1.delete();
				 f2.renameTo(new File(filename));


			}catch(Exception e)
			{
			  System.out.print(e.getStackTrace());
			}
 }
 public static void main(String[] args) {
  File f = new File("e:\\upload\\1.rtf");
  File d = new File("e:\\upload\\3.htm");
  convert(f,d);
  appendCon("e:\\upload\\3.htm");
 }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -