从java创建新word文档

从java创建新word文档,java,swing,event-handling,ms-word,Java,Swing,Event Handling,Ms Word,我想在java中单击按钮打开新的MS Word文档 你能给我推荐代码吗?我是按照代码来做的,但我认为它是打开现有文档而不是创建新文档 class OpenWordFile { public static void main(String args[]) { try { Runtime rt = Runtime.getRuntime(); rt.exec("cmd.exe /C start Employee.doc");

我想在java中单击按钮打开新的MS Word文档 你能给我推荐代码吗?我是按照代码来做的,但我认为它是打开现有文档而不是创建新文档

class OpenWordFile {

    public static void main(String args[]) {

        try {
            Runtime rt = Runtime.getRuntime();
            rt.exec("cmd.exe /C start Employee.doc");
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Exception occured" + ex);
        }

    }
}

您不能单独使用Java来实现这一点,至少如果您需要生成文档文件,您需要第三个工具库。请看一看,否则您可以使用运行时打开现有文件。

您不能单独使用Java来完成此操作,至少如果您需要生成文档文件,您需要第三个工具库(例如)。请看一看,否则您可以使用运行时打开现有文件。

只有注释没有任何文字

Runtime run = Runtime.getRuntime();
String lcOSName = System.getProperty("os.name").toLowerCase();
boolean MAC_OS_X = lcOSName.startsWith("mac os x");
if (MAC_OS_X) {
    run.exec("open " + file);
} else {
    //run.exec("cmd.exe /c start " + file); //win NT, win2000
    run.exec("rundll32 url.dll, FileProtocolHandler " + path);
}

只有没有任何文字的评论

Runtime run = Runtime.getRuntime();
String lcOSName = System.getProperty("os.name").toLowerCase();
boolean MAC_OS_X = lcOSName.startsWith("mac os x");
if (MAC_OS_X) {
    run.exec("open " + file);
} else {
    //run.exec("cmd.exe /c start " + file); //win NT, win2000
    run.exec("rundll32 url.dll, FileProtocolHandler " + path);
}

在最近发布的
(Java6.0)
中,Java提供了桌面类。该类的目的是在系统中打开与给定文件关联的应用程序。因此,如果您使用Word文档(.doc)调用open()方法,那么它会自动调用MS Word,因为它是与.doc文件关联的应用程序

我开发了一个小型Swing程序(尽管您可以将其开发为控制台应用程序),从用户处获取文档编号,并将文档调用到
MSWord
。假设是;文档存储为
文件名
,由
.doc
组成

下面给出的是Java程序,您可以按原样编译和运行它。确保将DIR变量更改为存储.doc文件的文件夹

下面是用Java打开Word文档的代码。。。这是从网络中摘录的……

import java.io.File;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

public class WordDocument extends JFrame {
    private JButton btnOpen;
    private JLabel jLabel1;
    private JTextField txtDocNumber;

    private static String DIR  ="c:\\worddocuments\\";   // folder where word documents are present.

    public WordDocument() {
       super("Open Word Document");
       initComponents();
    }

    private void initComponents() {
        jLabel1 = new JLabel();
        txtDocNumber = new JTextField();
        btnOpen = new JButton();

        Container c = getContentPane();
        c.setLayout(new java.awt.FlowLayout());
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Enter Document Number  : ");
        c.add(jLabel1);

        txtDocNumber.setColumns(5);
        c.add(txtDocNumber);

        btnOpen.setText("Open Document");
        btnOpen.addActionListener(new ActionListener() {      // anonymous inner class 
            public void actionPerformed(ActionEvent evt) {
                  Desktop desktop = Desktop.getDesktop();  
              try {
                File f = new File( DIR + txtDocNumber.getText()  +  ".doc");
                 desktop.open(f);  // opens application (MSWord) associated with .doc file
              }
              catch(Exception ex) {
                // WordDocument.this is to refer to outer class's instance from inner class
                JOptionPane.showMessageDialog(WordDocument.this,ex.getMessage(),"Error", JOptionPane.ERROR_MESSAGE);
              }
            }
        });

        c.add(btnOpen);

    } // initCompnents()

    public static void main(String args[]) {
          WordDocument wd = new WordDocument();
          wd.setSize(300,100);
          wd.setVisible(true);
    }
}

在最近发布的
(Java6.0)
中,Java提供了桌面类。该类的目的是在系统中打开与给定文件关联的应用程序。因此,如果您使用Word文档(.doc)调用open()方法,那么它会自动调用MS Word,因为它是与.doc文件关联的应用程序

我开发了一个小型Swing程序(尽管您可以将其开发为控制台应用程序),从用户处获取文档编号,并将文档调用到
MSWord
。假设是;文档存储为
文件名
,由
.doc
组成

下面给出的是Java程序,您可以按原样编译和运行它。确保将DIR变量更改为存储.doc文件的文件夹

下面是用Java打开Word文档的代码。。。这是从网络中摘录的……

import java.io.File;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

public class WordDocument extends JFrame {
    private JButton btnOpen;
    private JLabel jLabel1;
    private JTextField txtDocNumber;

    private static String DIR  ="c:\\worddocuments\\";   // folder where word documents are present.

    public WordDocument() {
       super("Open Word Document");
       initComponents();
    }

    private void initComponents() {
        jLabel1 = new JLabel();
        txtDocNumber = new JTextField();
        btnOpen = new JButton();

        Container c = getContentPane();
        c.setLayout(new java.awt.FlowLayout());
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Enter Document Number  : ");
        c.add(jLabel1);

        txtDocNumber.setColumns(5);
        c.add(txtDocNumber);

        btnOpen.setText("Open Document");
        btnOpen.addActionListener(new ActionListener() {      // anonymous inner class 
            public void actionPerformed(ActionEvent evt) {
                  Desktop desktop = Desktop.getDesktop();  
              try {
                File f = new File( DIR + txtDocNumber.getText()  +  ".doc");
                 desktop.open(f);  // opens application (MSWord) associated with .doc file
              }
              catch(Exception ex) {
                // WordDocument.this is to refer to outer class's instance from inner class
                JOptionPane.showMessageDialog(WordDocument.this,ex.getMessage(),"Error", JOptionPane.ERROR_MESSAGE);
              }
            }
        });

        c.add(btnOpen);

    } // initCompnents()

    public static void main(String args[]) {
          WordDocument wd = new WordDocument();
          wd.setSize(300,100);
          wd.setVisible(true);
    }
}

也许使用java.awt.Desktop会有所帮助

    File f = new File("<some temp path>\\file.docx");
    f.createNewFile();

    Desktop.getDesktop().open(f);
File f=新文件(\\File.docx”);
f、 createNewFile();
Desktop.getDesktop().open(f);
创建一个新的空文档,并使用systsem specifik程序打开该扩展。此解决方案的优点在于它适用于所有操作系统。。。只要操作系统有一个程序来查看该文件即可


虽然我怀疑您正在寻找对文件创建有更多控制权的semthing…

也许使用java.awt.Desktop会有所帮助

    File f = new File("<some temp path>\\file.docx");
    f.createNewFile();

    Desktop.getDesktop().open(f);
File f=新文件(\\File.docx”);
f、 createNewFile();
Desktop.getDesktop().open(f);
创建一个新的空文档,并使用systsem specifik程序打开该扩展。此解决方案的优点在于它适用于所有操作系统。。。只要操作系统有一个程序来查看该文件即可


尽管我怀疑您正在寻找对文件创建有更多控制权的semthing…

一切都在这里:一切都在这里:如果您想支持多个操作系统,此解决方案可能会变得混乱。如果您获得AD或Novell admin访问权限,则此解决方案可能会变得混乱:-)如果您想支持多个操作系统,此解决方案可能会变得混乱如果你有AD或Novell管理员权限,OS.right可能会很混乱:-)你试过了吗?我没有,但我想Word不会打开该文档,因为它只是空的。我可能错了。好评论!实际上,这取决于处理文件的软件。。。Word实际上接受空文件作为新文件。。。另一方面,一些文件格式将崩溃(PDF格式为一种)。您尝试过吗?我没有,但我想Word不会打开该文档,因为它只是空的。我可能错了。好评论!实际上,这取决于处理文件的软件。。。Word实际上接受空文件作为新文件。。。另一方面,一些文件格式将崩溃(PDF格式)。