在 JFrame 中打开 MS 文档

发布于 2024-10-15 19:35:14 字数 1539 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情场扛把子 2024-10-22 19:35:14

好吧!这是我的解决方案(使用 Eclipse SWT)将 MS Word 文档打开到 jframe 中:

import java.awt.Canvas;
import java.io.File;

import javax.swing.JFrame;

import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
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.FileDialog;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;


public class AbrirWordJFrame {
    static OleClientSite clientSite;
    static OleFrame frame;

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

        JFrame jframe=new JFrame("Mi jframe");
        final Canvas canvas=new Canvas();
        jframe.getContentPane().add(canvas);
        jframe.setSize(800, 600);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jframe.setVisible(true);

        display.asyncExec(new Runnable() {
            public void run() {
                Shell shell = SWT_AWT.new_Shell(display, canvas);
                shell.setSize(800, 600);

                //abrimos un word
                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.NULL, new File("prueba.doc"));
                    //esto abre un documento en blanco
//                  clientSite = new OleClientSite(frame, SWT.NONE, "Word.Document");
                    addFileMenu(frame);
                } catch (SWTError e) {
                    System.out.println("Unable to open activeX control");
                    display.dispose();
                    return;
                }
                //fin abrimos un word
                //abrimos un navegador
//              Browser browser = new Browser(shell, SWT.NONE);
//              browser.setLayoutData(new GridData(GridData.FILL_BOTH));
//              browser.setSize(800, 600);
//              browser.setUrl("http://www.google.com");
                //fin abrimos un navegador
                shell.open();

            }
        });

        //el titulo
//      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.NULL, new File("prueba.doc"));
//          //esto abre un documento en blanco
////            clientSite = new OleClientSite(frame, SWT.NONE, "Word.Document");
//          addFileMenu(frame);
//      } catch (SWTError e) {
//          System.out.println("Unable to open activeX control");
//          display.dispose();
//          return;
//      }
//      shell.setSize(800, 600);
//      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 menuFileOpen = new MenuItem(menuFile, SWT.CASCADE);
        menuFileOpen.setText("Open...");
        menuFileOpen.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                fileOpen();
            }
        });
        MenuItem menuFileExit = new MenuItem(menuFile, SWT.CASCADE);
        menuFileExit.setText("Exit");
        menuFileExit.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                shell.dispose();
            }
        });
    }

    static void fileOpen() {
        FileDialog dialog = new FileDialog(clientSite.getShell(), SWT.OPEN);
        dialog.setFilterExtensions(new String[] { "*.doc" });
        String fileName = dialog.open();
        if (fileName != null) {
            clientSite.dispose();
            clientSite = new OleClientSite(frame, SWT.NONE, "Word.Document", new File(fileName));
            clientSite.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);
        }
    }
}

资源:
Eclipse SWT: http://www.eclipse.org/swt

谢谢大家!

All right!, this is my solution (Using Eclipse SWT) to open a MS Word document into a jframe:

import java.awt.Canvas;
import java.io.File;

import javax.swing.JFrame;

import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
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.FileDialog;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;


public class AbrirWordJFrame {
    static OleClientSite clientSite;
    static OleFrame frame;

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

        JFrame jframe=new JFrame("Mi jframe");
        final Canvas canvas=new Canvas();
        jframe.getContentPane().add(canvas);
        jframe.setSize(800, 600);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jframe.setVisible(true);

        display.asyncExec(new Runnable() {
            public void run() {
                Shell shell = SWT_AWT.new_Shell(display, canvas);
                shell.setSize(800, 600);

                //abrimos un word
                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.NULL, new File("prueba.doc"));
                    //esto abre un documento en blanco
//                  clientSite = new OleClientSite(frame, SWT.NONE, "Word.Document");
                    addFileMenu(frame);
                } catch (SWTError e) {
                    System.out.println("Unable to open activeX control");
                    display.dispose();
                    return;
                }
                //fin abrimos un word
                //abrimos un navegador
//              Browser browser = new Browser(shell, SWT.NONE);
//              browser.setLayoutData(new GridData(GridData.FILL_BOTH));
//              browser.setSize(800, 600);
//              browser.setUrl("http://www.google.com");
                //fin abrimos un navegador
                shell.open();

            }
        });

        //el titulo
//      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.NULL, new File("prueba.doc"));
//          //esto abre un documento en blanco
////            clientSite = new OleClientSite(frame, SWT.NONE, "Word.Document");
//          addFileMenu(frame);
//      } catch (SWTError e) {
//          System.out.println("Unable to open activeX control");
//          display.dispose();
//          return;
//      }
//      shell.setSize(800, 600);
//      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 menuFileOpen = new MenuItem(menuFile, SWT.CASCADE);
        menuFileOpen.setText("Open...");
        menuFileOpen.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                fileOpen();
            }
        });
        MenuItem menuFileExit = new MenuItem(menuFile, SWT.CASCADE);
        menuFileExit.setText("Exit");
        menuFileExit.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                shell.dispose();
            }
        });
    }

    static void fileOpen() {
        FileDialog dialog = new FileDialog(clientSite.getShell(), SWT.OPEN);
        dialog.setFilterExtensions(new String[] { "*.doc" });
        String fileName = dialog.open();
        if (fileName != null) {
            clientSite.dispose();
            clientSite = new OleClientSite(frame, SWT.NONE, "Word.Document", new File(fileName));
            clientSite.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);
        }
    }
}

Resources:
Eclipse SWT: http://www.eclipse.org/swt

Thanks to everyone!

谈下烟灰 2024-10-22 19:35:14

Apache POI 是一个优秀且强大的库。我用过它几次,它确实对我有帮助。

Apache POI is a good and strong library. I used it several times and it really helped me.

在梵高的星空下 2024-10-22 19:35:14

但要做好准备,Apache POI 甚至无法读取 Microsoft 文档中的各种信息。我有许多来自客户的文件,POI 未正确处理。所以你应该测试足够。

But be prepared, Apache POI can not even read all sorts of information in a Microsoft document. I have many files from our customers, POI do not processed correctly. So you should test sufficient.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文