熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

Java操作Word,Excel,Access,IE,OutLook, PDF 等

2022-06-13   來源: Java核心技術 

  近來發現問這方面的問題特別多花了點時間找了一些資料並整理出一個簡單例子
  Java操作PDF
  Java操作WordExcelAccess
  參考:
  
  
  
  
  ?forum=&thread=&tstart=&trange=
  
  一個jacob操作Word的例子操作ExcelAccessOutlook的例子jacob的sample目錄中有
  import javaioFile;
  import *;
  import comjacobactiveX*;
  public class WordTest {
  
   public static void main(String[] args) {
     WordBean word=new WordBean();
     wordopenWord(true);
     wordcreateNewDocument();
     wordinsertText(Hello word);
   }
  }
  
  
  import comjacobactiveX*;
  import *;
  public class WordBean extends javaawtPanel
  {
   private ActiveXComponent MsWordApp = null;
   private Dispatch document = null;
   public WordBean()
   {
     super();
   }
   public void openWord(boolean makeVisible)
   {
  //Open Word if weve not done it already
     if (MsWordApp == null)
     {
       MsWordApp = new ActiveXComponent(WordApplication);
     }
  //Set the visible property as required
     Dispatchput(MsWordApp Visible
            new Variant(makeVisible));
   }
   public void createNewDocument()
   {
  //Find the Documents collection object maintained by Word
     Dispatch documents =
         Dispatchget(MsWordAppDocuments)toDispatch();
  //Call the Add method of the Documents collection to create
  //a new document to edit
     document = Dispatchcall(documentsAdd)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 =
         Dispatchget(MsWordAppSelection)toDispatch();
  //Put the specified text at the insertion point
     Dispatchput(selectionTexttextToInsert);
   }
   public void saveFileAs(String filename)
   {
     Dispatchcall(documentSaveAsfilename);
   }
   public void printFile()
   {
  //Just print the current document to the default printer
     Dispatchcall(documentPrintOut);
   }
   public void closeDocument()
   {
  // Close the document without saving changes
  // = wdDoNotSaveChanges
  // = wdSaveChanges
  // = wdPromptToSaveChanges
     Dispatchcall(document Close new Variant());
     document = null;
   }
   public void closeWord()
   {
     Dispatchcall(MsWordAppQuit);
     MsWordApp = null;
     document = null;
   }
  }
  

From:http://tw.wingwit.com/Article/program/Java/hx/201311/26588.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.