服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

java操作word,excel,access,ie,outlook, pdf 等


  近来发现问这方面的问题特别多,花了点时间,找了一些资料,并整理出一个简单例子
  java操作pdf http://www.lowagie.com/
  java操作word,excel,access
  参考:
  http://danadler.com/jacob/
  http://jakarta.apache.org/poi/
  http://www.onjava.com/pub/a/onjava/2003/01/22/poi.html
  http://www.csdn.net/develop/article/15/15311.shtm
  http://forum.java.sun.com/thread.jsp?forum=40&thread=382666&tstart=0&trange=15
  
  一个jacob操作word的例子,操作excel,access,outlook的例子jacob的sample目录中有
  import java.io.file;
  import com.jacob.com.*;
  import com.jacob.activex.*;
  public class wordtest {
  
   public static void main(string[] args) {
     wordbean word=new wordbean();
     word.openword(true);
     word.createnewdocument();
     word.inserttext("hello word.");
   }
  }
  
  
  import com.jacob.activex.*;
  import com.jacob.com.*;
  public class wordbean extends java.awt.panel
  {
   private activexcomponent mswordapp = null;
   private dispatch document = null;
   public wordbean()
   {
     super();
   }
   public void openword(boolean makevisible)
   {
  //open word if we've not done it already
     if (mswordapp == null)
     {
       mswordapp = new activexcomponent("word.application");
     }
  //set the visible property as required.
     dispatch.put(mswordapp, "visible",
            new variant(makevisible));
   }
   public void createnewdocument()
   {
  //find the documents collection object maintained by word
     dispatch documents =
         dispatch.get(mswordapp,"documents").todispatch();
  //call the add method of the documents collection to create
  //a new document to edit
     document = dispatch.call(documents,"add").todispatch();
   }
   public void inserttext(string texttoinsert)
   {
  // get the current selection within word at the moment. if
  // a new document has just been created then this will be at
  // the top of the new doc
     dispatch selection =
         dispatch.get(mswordapp,"selection").todispatch();
  //put the specified text at the insertion point
     dispatch.put(selection,"text",texttoinsert);
   }
   public void savefileas(string filename)
   {
     dispatch.call(document,"saveas",filename);
   }
   public void printfile()
   {
  //just print the current document to the default printer
     dispatch.call(document,"printout");
   }
   public void closedocument()
   {
  // close the document without saving changes
  // 0 = wddonotsavechanges
  // -1 = wdsavechanges
  // -2 = wdprompttosavechanges
     dispatch.call(document, "close", new variant(0));
     document = null;
   }
   public void closeword()
   {
     dispatch.call(mswordapp,"quit");
     mswordapp = null;
     document = null;
   }
  }

扫描关注微信公众号