2016-10-31 9 views
0

私はマイクロソフトワードの助けを借りてワード文書を読んでみたいですが、それは私のJavaフォームに限定されます。ドキュメントは、Microsoft Wordの助けを借りて、Javaフォームで開かれます。 enter image description hereどのように私はJavaフォームでMicrosoft Wordを埋め込むことができますか?

ユーザーが必要に応じてドキュメントを編集することができます。以下の画像のよう

は、私が何かを開発したいです。しかし、ドキュメントを保存したいときは、ドキュメントはリモートマシンに保存されます。

は、私は、次のコードでのMicrosoft Wordの助けを借りて、Word文書を開くことができます。

try { 
      /* if (Desktop.isDesktopSupported()) { 
        Desktop.getDesktop().open(new File("E:\\finger.docx")); 
      }*/ 
      Process p = Runtime.getRuntime() 
       .exec("rundll32 url.dll,FileProtocolHandler E:\\finger.docx"); 
      p.waitFor(); 
      System.out.println("Done."); 
     } catch (IOException ioe) { 
       ioe.printStackTrace(); 
     } 
     catch (InterruptedException ex) { 
       Logger.getLogger(JavaFromWord.class.getName()).log(Level.SEVERE, null, ex); 
      } 

しかし、私はJFormのワードビューアを埋め込むことができますどのように私は知りません。私を助けてください 。

+0

ない(現実的に)なんとか。参照:http://stackoverflow.com/questions/23225885/how-to-embed-a-windows-program-inside-java-component – kaqqao

+2

これは[XY問題](http://xyproblem.info)ですか? – Gowtham

+0

@Gowtham確かにXY問題です。 – DejaVuSansMono

答えて

0

私はこの仕事を成功させました。次のコードは、私がそれをするのを助けてくれます。

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.sql.Connection; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 
import java.util.ArrayList; 
import java.util.List; 

import javax.swing.JFrame; 
import javax.swing.JOptionPane; 

import org.eclipse.swt.SWT; 
import org.eclipse.swt.SWTError; 
import org.eclipse.swt.events.KeyEvent; 
import org.eclipse.swt.events.KeyListener; 
import org.eclipse.swt.events.SelectionAdapter; 
import org.eclipse.swt.events.SelectionEvent; 
import org.eclipse.swt.graphics.Color; 
import org.eclipse.swt.layout.FillLayout; 
import org.eclipse.swt.ole.win32.OLE; 
import org.eclipse.swt.ole.win32.OleClientSite; 
import org.eclipse.swt.ole.win32.OleFrame; 
import org.eclipse.swt.widgets.Display; 
import org.eclipse.swt.widgets.Event; 
import org.eclipse.swt.widgets.FileDialog; 
import org.eclipse.swt.widgets.Listener; 
import org.eclipse.swt.widgets.Menu; 
import org.eclipse.swt.widgets.MenuItem; 
import org.eclipse.swt.widgets.Shell; 

import com.connect.DBConnectionHandler; 

public class SWTMenuExample { 
    static OleClientSite clientSite; 
    static OleFrame frame; 
    static File file; 
    static Shell shell; 
    static KeyListener keyListener; 
    static Display display; 

    public static void main(final String[] args) { 
     display = new Display(); 
     shell = new Shell(display); 

     shell.setSize(800, 600); 
     shell.setText("Word Example"); 
     shell.setLayout(new FillLayout()); 

     try { 
      frame = new OleFrame(shell, SWT.NONE); 
      // esto abre un documento existente 
      // clientSite = new OleClientSite(frame, SWT.NONE, new 
      // File("Doc1.doc")); 
      // esto abre un documento en blanco 
      // clientSite = new OleClientSite(frame, SWT.NONE, "Word.Document"); 
      addFileMenu(frame); 
      System.out.println(" I am in run method "); 
     } catch (final SWTError e) { 
      System.out.println("Unable to open activeX control"); 
      display.dispose(); 
      return; 
     } 

     keyListener = new KeyListener() { 

      public void keyReleased(KeyEvent paramKeyEvent) { 

      } 

      public void keyPressed(KeyEvent paramKeyEvent) { 
       // TODO Auto-generated method stub 
       if (((paramKeyEvent.stateMask & SWT.CTRL) == SWT.CTRL) 
         && (paramKeyEvent.keyCode == 's')) { 
        JOptionPane.showMessageDialog(null, 
          "ctrl+s is pressed down initial ", 
          "Warning Message", JOptionPane.WARNING_MESSAGE); 
        if (file != null) { 
         clientSite.save(file, true); 
         fileSave(); 
         JOptionPane.showMessageDialog(null, 
           "ctrl+s is pressed down", "Warning Message", 
           JOptionPane.WARNING_MESSAGE); 
        } else 
         JOptionPane.showMessageDialog(null, "File is null", 
           "Warning Message", JOptionPane.WARNING_MESSAGE); 
       } 

      } 
     }; 

     display.addFilter(SWT.KeyDown, new Listener() { 

      public void handleEvent(Event e) { 
       if (((e.stateMask & SWT.CTRL) == SWT.CTRL) 
         && (e.keyCode == 's')) { 
        System.out.println("From Display I am the Key down !!" 
          + e.keyCode); 
       } 
      } 
     }); 

     final Color green = display.getSystemColor(SWT.COLOR_GREEN); 
     final Color orig = shell.getBackground(); 

     shell.addKeyListener(new KeyListener() { 
      public void keyReleased(KeyEvent e) { 
       if (((e.stateMask & SWT.CTRL) == SWT.CTRL) 
         && (e.keyCode == 's')) { 
        shell.setBackground(orig); 
        System.out.println("Key up !!"); 
       } 
      } 

      public void keyPressed(KeyEvent e) { 
       if (((e.stateMask & SWT.CTRL) == SWT.CTRL) 
         && (e.keyCode == 's')) { 
        shell.setBackground(green); 
        System.out.println("Key down !!"); 
       } 
      } 
     }); 

     shell.open(); 
     while (!shell.isDisposed()) { 
      if (!display.readAndDispatch()) { 
       display.sleep(); 
      } 
     } 
     display.dispose(); 
    } 

    static void addFileMenu(OleFrame frame) { 
     final Shell shell = frame.getShell(); 
     Menu menuBar = shell.getMenuBar(); 
     if (menuBar == null) { 
      menuBar = new Menu(shell, SWT.BAR); 
      shell.setMenuBar(menuBar); 
     } 
     MenuItem fileMenu = new MenuItem(menuBar, SWT.CASCADE); 
     fileMenu.setText("&File"); 
     Menu menuFile = new Menu(fileMenu); 
     fileMenu.setMenu(menuFile); 
     frame.setFileMenus(new MenuItem[] { fileMenu }); 

     MenuItem menuFileCreate = new MenuItem(menuFile, SWT.CASCADE); 
     menuFileCreate.setText("Create File"); 
     menuFileCreate.addSelectionListener(new SelectionAdapter() { 
      public void widgetSelected(SelectionEvent e) { 
       fileCreate(); 
      } 
     }); 

     MenuItem menuFileOpen = new MenuItem(menuFile, SWT.CASCADE); 
     menuFileOpen.setText("Open File"); 
     menuFileOpen.addSelectionListener(new SelectionAdapter() { 
      public void widgetSelected(SelectionEvent e) { 
       fileOpen(); 
      } 
     }); 

     MenuItem menuFileSave = new MenuItem(menuFile, SWT.CASCADE); 
     menuFileSave.setText("Save File"); 
     menuFileSave.addSelectionListener(new SelectionAdapter() { 
      public void widgetSelected(SelectionEvent e) { 
       if (file != null) { 
        clientSite.save(file, true); 
        fileSave(); 
       } 
      } 
     }); 

     MenuItem menuFileClose = new MenuItem(menuFile, SWT.CASCADE); 
     menuFileClose.setText("Close File"); 
     menuFileClose.addSelectionListener(new SelectionAdapter() { 
      public void widgetSelected(SelectionEvent e) { 
       if (clientSite != null) { 
        clientSite.dispose(); 
        file = null; 
       } 
      } 
     }); 
    } 

    static void fileCreate() { 
     if (clientSite != null) 
      clientSite.dispose(); 
     if (file != null) 
      file.delete(); 
     String fileName = JOptionPane 
       .showInputDialog("Please input the name of you file: "); 
     if (fileName != null) { 
      if (fileName.trim().equals("")) { 
       JFrame frame = new JFrame(
         "JOptionPane showMessageDialog example"); 

       JOptionPane.showMessageDialog(frame, 
         "File Name must have some value", "Warning Message", 
         JOptionPane.WARNING_MESSAGE); 
      } else { 

       Connection conn = DBConnectionHandler.getConnection(); 
       PreparedStatement pstmt; 
       try { 
        pstmt = conn 
          .prepareStatement("insert into FILEDATA(PR_KEY, FILENAME, FILEDATA) values (seq_file.nextval,?, EMPTY_BLOB())"); 
        pstmt.setString(1, fileName + ".docx"); 
        pstmt.executeUpdate(); 
       } catch (SQLException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
     } 

    } 

    static void fileOpen() { 
     List<String> FileName = new ArrayList<String>(); 
     try { 
      Connection conn = DBConnectionHandler.getConnection(); 
      Statement stmt = conn.createStatement(); 
      ResultSet rset = stmt.executeQuery("select FILENAME from FILEDATA"); 
      while (rset.next()) 
       FileName.add(rset.getString(1)); 
      System.out.println("List Size "+FileName.size()); 
      if (clientSite != null) 
       clientSite.dispose(); 
      shell.setVisible(false); 
      if (file != null) 
       file.delete(); 
      SWTMenuExample swtClass = new SWTMenuExample(); 
      NewJFrame form = new NewJFrame(FileName, clientSite, shell, frame, 
        swtClass); 
      form.setVisible(true); 
      form.setSize(400, 400); 

     } catch (SQLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     /* 
     * FileDialog dialog = new FileDialog(shell, SWT.OPEN); 
     * dialog.setFilterExtensions(new String[] { "*.docx" }); String 
     * fileName = dialog.open(); if (fileName != null) { if (clientSite != 
     * null) clientSite.dispose(); file = new File(fileName); clientSite = 
     * new OleClientSite(frame, SWT.NONE, "Word.Document", file); 
     * clientSite.addKeyListener(keyListener); 
     * clientSite.doVerb(OLE.OLEIVERB_INPLACEACTIVATE); } 
     */ 
    } 

    void fileOpenFromDrive(File happyFile) { 
     file = happyFile; 
     display.asyncExec(new Runnable() { 
      @Override 
      public void run() { 
       shell.setVisible(true); 
       if (clientSite != null) 
        clientSite.dispose(); 

       clientSite = new OleClientSite(frame, SWT.NONE, 
         "Word.Document", file); 
       clientSite.addKeyListener(keyListener); 
       clientSite.doVerb(OLE.OLEIVERB_INPLACEACTIVATE); 

       shell.addListener(SWT.Close, new Listener() { 
        public void handleEvent(Event event) { 
         if (file != null) 
          file.delete(); 
         shell.dispose(); 
        } 
       }); 
      } 
     }); 

    } 

    static void fileSave() { 
     try { 
      FileInputStream fis = new FileInputStream(file); 
      Connection conn = DBConnectionHandler.getConnection(); 

      PreparedStatement pstmt = conn 
        .prepareStatement("update FILEDATA set FILEDATA = ? where FILENAME = ?"); 
      pstmt.setBinaryStream(1, fis, (int) file.length()); 
      pstmt.setString(2, file.getName()); 
      pstmt.executeUpdate(); 
     } catch (SQLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
} 

それは、次の写真のようなとしてdocxファイルを開きます:

enter image description here

-1

Actaullyが、あなたはJavaのGUIアプリケーションのあらゆるない - あなたのプロジェクトフレームを表示することはできません、それはactaully非現実的である - あなただけのJInternalFrameでJavaフレームを表示することができます

0

他の人が指摘したように、この音XY問題のように、あなたはすでに、最良のソリューションがMicrosoft Wordを使用していると判断しているので、ユーザーインターフェイスに書式設定されたテキストを組み込んでいるように見える実際の目標を尋ねる代わりに、Microsoft Wordを埋め込む方法を尋ねています。

これを行うには良い方法は、HTMLコンテンツを含むJEditorPaneのである:

import java.awt.EventQueue; 
import javax.swing.JEditorPane; 
import javax.swing.JFrame; 
import javax.swing.JScrollPane; 

public class HtmlPane { 
    static void show() { 
     JEditorPane content = new JEditorPane("text/html", 
      "<p>" + 
      "The process of finger enrollment is now to be done in database." + 
      " Previously it was done in file template." + 
      " There are several reasons behind this:" + 
      "<ol>" + 
      "<li>" + 
      "Time:<br>" + 
      "--------" + 
      "<p>" + 
      "The process of verification in file template based method takes" + 
      " 6.9 s on average. On the other hand, in database based method," + 
      " it takes only 3.5 s." + 
      "<p>" + 
      "The code for determining time is as follows:" + 
      "<blockquote>" + 
      "<pre>" + 
      "Dim StartTime, Elapsed Time\n" + 
      "StartTime = Timer\n" + 
      "ElapsedTime = Timer.CheckTime\n" + 
      "</pre>" + 
      "</blockquote>" + 
      "</ol>"); 

     content.setEditable(false); 

     JFrame frame = new JFrame("finger"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(new JScrollPane(content)); 

     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     EventQueue.invokeLater(() -> show()); 
    } 
} 

(注:SwingのHTMLレンダラはHTML 3.2をサポート)を使用すると、HTMLに精通していない場合

を私はあなたにそれを学ぶよう勧めます。基本的なHTMLは非常にです。また、プロの開発ではjavadocを書く必要があり、すべてのjavadocはHTML形式であるため、効果的なJavaプログラマであるためには基本的なHTMLを知る必要があります。

+0

私はJFormに限定されているMicrosoft Wordで単語の文書を開きたいと思います。 –

関連する問題