Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从JButton-javaswing打开excel工作表_Java_Swing_Excel - Fatal编程技术网

从JButton-javaswing打开excel工作表

从JButton-javaswing打开excel工作表,java,swing,excel,Java,Swing,Excel,我正在使用swing设计表单。如果在我的表单中单击了报表按钮,它必须从我的D驱动器打开预定义格式的excel工作表。在按钮操作侦听器中必须编写哪些代码 谢谢..您可以使用运行时类在窗口中使用默认程序打开excel String filePath = "D:\\test.xls"; Process p = Runtime.getRuntime() .exec("rundll32 url.dll,FileProtocolHandler " + filePath); 您还可以使用Desk

我正在使用swing设计表单。如果在我的表单中单击了报表按钮,它必须从我的D驱动器打开预定义格式的excel工作表。在按钮操作侦听器中必须编写哪些代码


谢谢..

您可以使用
运行时
类在
窗口中使用默认程序打开excel

String filePath = "D:\\test.xls";
Process p = 
  Runtime.getRuntime()
   .exec("rundll32 url.dll,FileProtocolHandler " + filePath);
您还可以使用
Desktop
API,如下所示

String filePath = "D:\\test.xls";
Desktop dt = Desktop.getDesktop();
dt.open(new File(filePath));

您可以使用
Runtime
类在
Windows

String filePath = "D:\\test.xls";
Process p = 
  Runtime.getRuntime()
   .exec("rundll32 url.dll,FileProtocolHandler " + filePath);
您还可以使用
Desktop
API,如下所示

String filePath = "D:\\test.xls";
Desktop dt = Desktop.getDesktop();
dt.open(new File(filePath));

我建议您看看awt中的Desktop类。有一种方法可以帮助你

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;

public class OpenFile {

    public static void main(String[] args) {
        File file = new File("c:\\appNominalJournalFix.txt");
        try {
            Desktop.getDesktop().open(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

我建议您看看awt中的Desktop类。有一种方法可以帮助你

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;

public class OpenFile {

    public static void main(String[] args) {
        File file = new File("c:\\appNominalJournalFix.txt");
        try {
            Desktop.getDesktop().open(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

可能重复的可能重复的是,这将不会工作,除非在windows中。在linux中,使用openoffice或libreoffice,但找不到RundL32。是的,这在windows中不起作用。在linux中,使用openoffice或libreoffice,但找不到RundL32。真的吗?我把它放在一个主方法中,效果很好。我将粘贴整个类,但对我来说,我确定它可以编译。真的吗?我把它放在一个主方法中,效果很好。我将粘贴整个类,但对我来说,我确信它可以编译。